使用 Mockito 模拟新对象创建

Mock new object creation with Mockito

每当为 class 创建一个新对象时,我都需要模拟一个 class 对象。我没有使用任何 java 框架来进行依赖注入。下面是我的代码。

Addition.java

public int sum(int a, int b) {
    return a + b;
}

Application.java

public int doOperation(int a, int b) {
    Addition addition = new Addition();
    return addition.sum(a, b);
}

ApplicationTest.java

@RunWith(PowerMockRunner.class)
@PrepareForTest(Addition.class)

        Addition addition = PowerMockito.mock(Addition.class);
        PowerMockito.when(addition.sum(ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt())).thenReturn(15);

        PowerMockito.whenNew(Addition.class).withNoArguments().thenReturn(addition);

        Application applicationMock = PowerMockito.mock(Application.class);
        int res = applicationMock.doOperation(1, 5);

        assertEquals(res, 15);

Application.java 我正在创建 Addition class 实例。我需要 return 模拟对象而不是新对象。当我 运行 我的应用程序时,我得到 0 作为 res.

java.lang.AssertionError: 
Expected :0
Actual   :15
<Click to see difference>


    at org.junit.Assert.fail(Assert.java:88)
    at org.junit.Assert.failNotEquals(Assert.java:834)
    at org.junit.Assert.assertEquals(Assert.java:645)
    at org.junit.Assert.assertEquals(Assert.java:631)
    at com.dbs.poc.PowerMockAdditionTest.sumWithMocks(PowerMockAdditionTest.java:38)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:326)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:89)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:310)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:131)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.access0(PowerMockJUnit47RunnerDelegateImpl.java:59)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner$TestExecutorStatement.evaluate(PowerMockJUnit47RunnerDelegateImpl.java:147)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.evaluateStatement(PowerMockJUnit47RunnerDelegateImpl.java:107)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:298)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:218)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:160)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:134)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:136)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:117)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:57)
    at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

你漏掉了一行。试试这个。

 Application applicationMock = PowerMockito.mock(Application.class);
 PowerMockito.when(applicationMock.doOperation(anyInt(), anyInt())).thenCallRealMethod();
 int res = applicationMock.doOperation(1, 5);

您已将应用程序声明为 Mock。并且没有定义 doOperation 方法的行为。因此控件不会进入该方法,因此没有任何效果。

这是完整的代码,

@RunWith(PowerMockRunner.class)
@PrepareForTest({Application.class})
public class ApplicationTest {

    @Test
    public void test() throws Exception {
        Addition addition = PowerMockito.mock(Addition.class);
        PowerMockito.when(addition.sum(ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt())).thenReturn(15);

    PowerMockito.whenNew(Addition.class).withNoArguments().thenReturn(addition);

        Application applicationMock = PowerMockito.mock(Application.class);
        when(applicationMock.doOperation(anyInt(), anyInt())).thenCallRealMethod();
        int res = applicationMock.doOperation(1, 5);

        assertEquals(res, 15);
    }
}

如果您只想测试加法。您不需要模拟应用程序 class。而是创建应用程序的新实例 class。这样您就不需要修复,您当前的代码就足够了。

使用

Application application = new Application();

而不是

Application applicationMock = PowerMockito.mock(Application.class);

如果您有正当理由使用模拟而不是真实实例,那么这就是解决方案。

这是 problem/missing link:

Application applicationMock = PowerMockito.mock(Application.class);
int res = applicationMock.doOperation(1, 5);

applicationMock.doOperation(x, y)是什么意思? - 什么都没有 returns 0。 (您可以配置 (Power)mock(ito) 来抛出异常 -> "strict mock")

你想要的是:

a) 测试 "real Application"(无模拟):

Application testApp = new Applicaiton();
int res = testApp.doOperation(1, 5);
// ...

或者 b) "program" 你的模拟,到 fake/copy 真正的实现(例如 @pvpkiran 所概述的):

Application applicationMock = PowerMockito.mock(Application.class);
PowerMockito.when(applicationMock.doOperation(anyInt(), anyInt())).then...