Powermockito static method call

105

Powermockito static method call -

@SpringBootTest
@RunWith(PowerMockTestRunner.class)
@PrepareForTest(value = B.class)
public class TestClass {

    @Test
    public void testBAdmin() {
        String auditUser = "admin";
        Timestamp timestamp = new Timestamp(1577447182l);

        PowerMockito.mockStatic(B.class);
        //You can mock method here, if you need return value like this
        //when(B.testB(timestamp)).thenReturn("some_value");

        A.testA(auditUser, timestamp);

        PowerMockito.verifyStatic(B.class);
        B.testB(timestamp);
    }
    @Test
    public void testBNotAdmin() {
        String auditUser = "not_admin";
        Timestamp timestamp = new Timestamp(1577447182l);

        PowerMockito.mockStatic(B.class);
        //You can mock method here, if you need return value like this
        //when(B.testB(timestamp)).thenReturn("some_value");

        A.testA(auditUser, timestamp);

        PowerMockito.verifyZeroInteractions(B.class);
    }
}
class A {
    public static void testA(String auditUser, Timestamp timestamp) {
        if ("admin".equalsIgnoreCase(auditUser)) {
            B.testB(timestamp);
        }
    }
}

class B {
    public static void testB(Timestamp timestamp) {
//...some logic...//
    }
}

Comments

Submit
0 Comments