gpt4 book ai didi

java - 如何使用 EasyMock 从模拟接口(interface)调用方法

转载 作者:行者123 更新时间:2023-11-30 07:09:31 25 4
gpt4 key购买 nike

我正在为一个类编写 Junit 单元测试,并且在以下行中收到 java.lang.NullPointerException :

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

我认为(但我不确定)它与我从模拟接口(interface)中调用的方法(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;
}
}

声明方法的接口(interface)

public interface LineConfigurationHandler {

DeviceControlHandler getDeviceControlHandler();

//other Method declaration ...
}

DeviceControlHandler.class

public interface DeviceControlHandler extends Serializable{

List<DeviceParameter> getDeviceParameters(String deviceId);

//Other methods declaration ...
}

最佳答案

这并不简单,但非常确定:

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);

关于java - 如何使用 EasyMock 从模拟接口(interface)调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39451094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com