[英]Can't figure out why verification doesn't work properly
I have a strange code of tests which always green. 我有一个奇怪的测试代码,总是绿色的。 At the same time one of the tests shouldn't be green. 同时其中一个测试不应该是绿色的。 Please see the code below. 请参阅下面的代码。
It's a class which I need to test 这是我需要测试的课程
public class A {
private String param;
public void print(){
System.out.println(this.param);
}
public static void printHello(){
System.out.println("Hello!");
}
}
And test for itself 并测试自己
import org.mockito.Spy;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.powermock.api.easymock.PowerMock.replay;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.verifyStatic;
import static org.powermock.reflect.Whitebox.invokeMethod;
public class ATest {
@Spy
private A a = new A();
@BeforeMethod
public void setUp() {
initMocks(this);
}
@Test
public void test() {
// When
a.print();
// Than
verify(a, times(1)).print();
}
@Test
@PrepareForTest(A.class)
public void testStatic() throws Exception {
mockStatic(A.class);
replay();
invokeMethod(A.class, "printHello");
verifyStatic(times(10)); // must be fail
}
}
Obviously method testStatic() should fail because it doesn't invokes 10 times. 显然方法testStatic()应该失败,因为它不会调用10次。
UPD UPD
Here is my new version of test 这是我的新版测试
@PrepareForTest(A.class)
public class ATest extends PowerMockTestCase {
@Spy
private A a = new A();
@BeforeMethod
public void setUp() {
initMocks(this);
}
@Test
public void test() {
// When
a.print();
// Than
verify(a, times(1)).print();
}
@Test
@PrepareForTest(A.class)
public void testStatic() throws Exception {
mockStatic(A.class);
replay();
invokeMethod(A.class, "printHello");
verifyStatic(times(10)); // must be fail
}
@ObjectFactory
public IObjectFactory getObjectFactory() {
return new org.powermock.modules.testng.PowerMockObjectFactory();
}
}
And error stacktrace 和错误堆栈跟踪
org.mockito.exceptions.verification.TooManyActualInvocations:
a.print();
Wanted 1 time:
-> at com.aaron.simple.ATest.test(ATest.java:37)
But was 2 times. Undesired invocation:
-> at com.aaron.simple.ATest.test(ATest.java:34)
I think the test class should be annotated with @RunWith(PowerMockRunner.class)
我认为测试类应该用@RunWith(PowerMockRunner.class)
注释
Without this Powermock basically does nothing at all. 没有这个Powermock基本上什么都不做。
Ok, you use TestNG. 好的,你使用TestNG。 It still needs the equivalent information. 它仍然需要相同的信息。 The setup is described here in the section "Configure TestNG to use the PowerMock object factory". 此处的“将TestNG配置为使用PowerMock对象工厂”一节中介绍了该设置 。
JUnit creates a new instance for each test methods, a contrario TestNG creates a single test instance for the whole suite. JUnit为每个测试方法创建一个新实例,相反,TestNG为整个套件创建一个测试实例。 That means that your instance variables are not reset between the test methods. 这意味着您的实例变量不会在测试方法之间重置。
The test should renew the instance fields, or for Mockito reset them ( Mockito.reset(...)
). 测试应该更新实例字段,或者为Mockito重置它们( Mockito.reset(...)
)。
Note that you can find a few TestNG utility classes in the Mockito project, they are not yet distributed however but help to create mocks in the same fashion as with JUnit. 请注意,您可以在Mockito项目中找到一些TestNG实用程序类 ,但它们尚未分发,但有助于以与JUnit相同的方式创建模拟。 However since the mock is created with PowerMock it's a different story. 然而,由于模拟是使用PowerMock创建的,因此它是一个不同的故事。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.