如何使用EasyMock从模拟接口调用方法

时间:2016-09-12 13:08:30

标签: java unit-testing junit easymock

我正在为一个类编写一个Junit单元测试,我在下一行得到了一个java.lang.NullPointerException:

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

我认为(我不确定)它与我在模拟界面中调用的方法(getDeviceControlHandler)有关。因为我在上述行之前添加了这行代码:

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 个答案:

答案 0 :(得分:1)

这不简单,但非常确定:

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

该行包含两个可以抛出NPE的项:

A)lineConfigurationHandlerMock - &gt;该对象可以为NULL

B).getDeviceControlHandler() - &gt;该方法可以返回NULL

那就是它。你可以做简单的打印输出,比如

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

确定哪一个为空。在你的情况下,我认为你缺少lineConfigurationHandlerMock对象的设置:你必须告诉它在调用getDeviceControlHandler()时要返回什么。

为了做到这一点,首先必须创建另一个模拟对象,该对象应在调用getDeviceControlHandler()时返回。而另一个模拟,你必须配置调用getDeviceParameters()!

换句话说:你不能指定像&#34; mock.getA()。doSomething()&#34; - 相反,你需要另一个&#34; mockedA&#34;你告诉&#34; doSomething()&#34 ;;然后你告诉&#34; mock&#34; getA()应该返回&#34; mockedA&#34;。

更新:我不熟悉这些注释;我习惯在裸机模式下使用&#34; EasyMock&#34 ;;像

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

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