简体   繁体   English

无法弄清楚为什么验证不能正常工作

[英]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.

相关问题 无法弄清楚为什么无法正确解决 - Can't figure out why it isn't resolving properly 无法弄清楚为什么此正则表达式不起作用(带有“开始于”限定词的多行文本) - Can't figure out why this regex doesn't work (multiple lines of text with “starts with” qualifier) 我不知道为什么这个简单的videoView演示不起作用 - I can't figure it out why this simple videoView demo doesn't work 无法弄清楚为什么对象的 ArrayList 没有正确排序 - Can't figure out why is ArrayList of objects not sorted properly 无法弄清楚为什么程序无法正常运行 - Can't figure out why the program will not run properly 计算错误,无法找出原因 - Calculation incorrect and can't figure out why 为什么Java编译器不能解决这个问题呢? - Why can't the Java compiler figure this out? 无法弄清楚为什么Mongo Database应用程序在创建jar可执行文件后无法正常工作 - Can't figure out why Mongo Database app won't work correctly after creating a jar executable 找到总和为1000的pythogorean三联体的程序,无法弄清楚为什么它不起作用? - Program to find pythogorean triplet whose sum is 1000, cannot figure out why it doesn't work? Java的。 帮助弄清楚为什么我的代码不起作用 - Java. Help figure out why my code doesn't work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM