简体   繁体   中英

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.

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)

Without this Powermock basically does nothing at all.

Ok, you use TestNG. It still needs the equivalent information. The setup is described here in the section "Configure TestNG to use the PowerMock object factory".

JUnit creates a new instance for each test methods, a contrario TestNG creates a single test instance for the whole suite. 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(...) ).

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. However since the mock is created with PowerMock it's a different story.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM