【问题标题】:How to call a method from a mocked interface using EasyMock如何使用 EasyMock 从模拟接口调用方法
【发布时间】:2016-09-12 13:08:30
【问题描述】:

我正在为一个类编写一个 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 ...
}

【问题讨论】:

    标签: java unit-testing junit easymock


    【解决方案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()”那样指定——相反,你需要另一个你告诉“doSomething()”的“mockedA”;然后你告诉“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);
    

    【讨论】:

    • 感谢您的帮助。实际上我已经完成了打印输出并且 getDeviceControHandler() 返回 NULL。现在我想实现您在答案的最后一段中解释的内容,但我不知道该怎么做。你能告诉我它是如何实现的吗?谢谢
    • 我增强了我的回答;但请理解我不喜欢使用这些注释;所以你必须从那里开始。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多