I can’t understand why the check is not working properly

I have a weird test code that is always green. At the same time, one of the tests should not be green. See code below.

This is the class I need to check.

public class A {
    private String param;

    public void print(){
        System.out.println(this.param);
    }

    public static void printHello(){
        System.out.println("Hello!");
    }
}

And check yourself

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, the testStatic () method should fail because it does not call 10 times.

UPD

Here is my new test version

@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 stacktrace error

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)
+4
source share
2 answers

JUnit creates a new instance for each test method, contrario TestNG creates a single test instance for the entire package. This means that your instance variables are not reset between test methods.

Mockito reset them (Mockito.reset(...)).

, TestNG Mockito, , mocks , JUnit. , PowerMock, .

0

, @RunWith(PowerMockRunner.class)

Powermock .

, TestNG. - . " TestNG PowerMock factory".

+2

Source: https://habr.com/ru/post/1547588/


All Articles