2016-09-12 5 views
1

Я пишу модульный тест Junit для класса и я получаю java.lang.NullPointerException на следующей строке:Как вызвать метод из издевались интерфейса с помощью EasyMock

expect(lineConfigurationHandlerMock.getDeviceControlHandler().getDeviceParameters(item1)).andReturn(myDeviceParameters); 

Я думаю, что (I я не уверен, хотя), что он имеет какое-то отношение к методу (getDeviceControlHandler), который я вызываю изнутри издевавшегося интерфейса. Потому что я добавил эту строку кода перед submentioned линии:

Assert.assertNotNull(comLineConfigurationHandlerMock.getDeviceControlHandler()); 

И я имею следующую ошибку:

java.lang.AssertionError

Я застрял здесь и действительно нужна помощь.

Спасибо заранее.

Заброшенная Исключение:

java.lang.NullPointerException 
at de.myproject.project.classTest.testGetParameters(classTest.java:123) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
at java.lang.reflect.Method.invoke(Unknown Source) 
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) 
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) 
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) 

Вот письменный тест:

public class classTest { 

// class under test 
private classUnderTest classUnderTest; 

private LineConfigurationHandler LineConfigurationHandlerMock; 

private IMocksControl mocksControl; 

List<DeviceParameter> myDeviceParameters; 
DeviceParameter deviceParameter1; 
DeviceParameter deviceParameter2; 

@Before 
public void setUp() throws Exception 
{ 
    mocksControl = EasyMock.createControl(); 

    LineConfigurationHandlerMock = mocksControl.createMock(LineConfigurationHandler.class); 

    classUnderTest = new classUnderTest(); 
    classUnderTest.setLineConfigurationHandler(LineConfigurationHandlerMock); 

    String item1 = "item1"; 

    myDeviceParameters = new ArrayList<DeviceParameter>(); 
    myDeviceParameters.add(deviceParameter1); 
    myDeviceParameters.add(deviceParameter2); 

    //Other stuff 
} 

@Test 
public void testGetParameters() 
{ 

    expect(LineConfigurationHandlerMock.getDeviceControlHandler().getDeviceParameters(item1)).andReturn(myDeviceParameters); 

    mocksControl.replay(); 

    //Some code ..... 
} 
} 

Вот класс под тест:

public Class ClassUnderTest 
    { 
    @Inject 
    private LineConfigurationHandler lineConfigurationHandler; 

    public List<DeviceParameter> getDeviceParameters(String deviceId) 
    { 
     // Method implementation 
    } 

    @Required 
    public void setLineConfigurationHandler(LineConfigurationHandler lineConfigurationHandler) 
    { 
     this.lineConfigurationHandler = lineConfigurationHandler; 
    } 
    } 

интерфейс, в котором объявлен метод

public interface LineConfigurationHandler { 

DeviceControlHandler getDeviceControlHandler(); 

//other Method declaration ... 
} 

DeviceControlHandler.class

public interface DeviceControlHandler extends Serializable{ 

List<DeviceParameter> getDeviceParameters(String deviceId); 

//Other methods declaration ... 
} 

ответ

1

Это не просто, но очень детерминированный:

expect(lineConfigurationHandlerMock.getDeviceControlHandler().getDeviceParameters(item1)).andReturn(myDeviceParameters); 

Эта строка содержит две предметов, которые могут бросить NPE:

A) lineConfigurationHandlerMock - -> этот объект может быть NULL

B) .getDeviceControlHandler() -> этот метод может возвращать NULL

Вот и все. Вы можете делать простые распечатки, например

System.out.println("mock: " + lineConfigurationHandlerMock) 
System.out.println("handler: " + lineConfigurationHandlerMock.getDeviceControlHandler()) 

, чтобы определить, какой из них равен нулю. В вашем случае, я думаю, вам не хватает настройки для объекта lineConfigurationHandlerMock: вы должны сказать ему, что вернуть, когда вызывается getDeviceControlHandler().

Для этого вам сначала нужно создать еще один макет объекта, который должен быть возвращен при вызове getDeviceControlHandler(). И этот другой макет, вы должны настроить для вызова getDeviceParameters()!

Другими словами: вы не можете указать «mock.getA(). DoSomething()» - вместо этого вам нужно другое «mockedA», которое вы укажете «doSomething()»; а затем вы скажете «mock», что getA() должен возвращать «mockedA».

Обновление: Я не знаком с этими аннотациями; Я привык использовать «EasyMock в режиме голого металла»; например

SomeObject innerMock = EasyMock.createMock(SomeObject); 
expect(innerMock.doSomething()).andReturn("who cares"); 

SomeOther outerMock = EasyMock.createMock(SomeOther); 
expect(outerMock.getDeviceControlHandler("sounds familiar")).andReturn(innerMock); 
+0

Благодарим за помощь. На самом деле я сделал распечатки, и getDeviceControHandler() возвращает NULL. Теперь я хочу реализовать то, что вы объяснили в последнем абзаце своего ответа, но я не знаю, как это сделать. Не могли бы вы рассказать мне, как это можно реализовать? Спасибо –

+0

Я повысил свой ответ; но, пожалуйста, поймите, что я не использую эти аннотации; так что вам нужно проложить свой путь оттуда. – GhostCat

+0

Он работает как шарм !!! Большое спасибо за вашу помощь ! :) –

Смежные вопросы