验证使用Mockito / PowerMock调用该方法

时间:2017-06-08 16:42:17

标签: mockito powermock

我的任务是通过单元测试复杂类来覆盖,该类与某些API交互。

以下是该类的一部分的简化结构。

我需要做的就是验证testMethod()内部是否正在使用正确的变量调用print()方法。

第1课:

public abstract class Class1 {

   public static Class2 class2;

   public void init() {
       //here initializes class2 variable, but for testing with mocks it doesn't needed
   }

   public void testMethod(String str) {
       class2.print(str);
   }
}

第2课:

public class Class2 {

    public void print(String str) {
        System.out.println(str);
    }

}

以下是我理解它的解决方案:

@RunWith(MockitoJUnitRunner.class)
public class FirstTest {

   @Test
   public void test() {
      Class1 class1 = Mockito.spy(Class1.class);
      Class2 class2 = Mockito.mock(Class2.class);
      doNothing().when(class2).print(anyString());
      Mockito.doCallRealMethod().when(class1).testMethod("abc");
      verify(class2).print("abc");
    }
}

它返回了这样的异常:

Wanted but not invoked:
class2.print("abc");
-> at FirstTest.test(FirstTest.java:50)
Actually, there were zero interactions with this mock.

Wanted but not invoked:
class2.print("abc");
-> at FirstTest.test(FirstTest.java:50) 
Actually, there were zero interactions with this mock.

at FirstTest.test(FirstTest.java:50)
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.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.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.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

当然,我对Mockito框架的知识存在很大差距。 也许有人会写出正确的解决方案。

Eddited代码:

@RunWith(MockitoJUnitRunner.class)

public class FirstTest {

@InjectMocks
Class1 class1 = Mockito.spy(Class1.class);

@Mock
Class2 class2;

@Test
public void test() {
    doNothing().when(class2).print("abc");
    class1.testMethod("abc");
    verify(class2).print(anyString());
}
}

0 个答案:

没有答案