2016-09-12 33 views
1

我寫了一類一個Junit的單元測試,我才能上以下行顯示java.lang.NullPointerException一個嘲笑接口調用一個方法:如何使用EasyMock的

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

我認爲(我我不確定)它與我在模擬界面中調用的方法(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()」;然後你告訴「模擬」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

它就像一個魅力!非常感謝您的幫助 ! :) –