Home > Net >  Spy a class without PowerMock
Spy a class without PowerMock

Time:05-23

I don't want to use powermock anymore. Because junit5 started mocking static classes. So i am trying to get rid of powermock methods.

While i was using PowerMock, i could easily spy a class that has a private constructor, and then i was calling the static methods.

This is a part of my code ( When i was using PowerMock )

@RunWith(PowerMockRunner.class)
@PrepareForTest(MessageValidationUtils.class)
public class MessageValidationServiceTest {

    @Mock
    private CheckpointCustomerService checkpointCustomerService;


    @Mock
    private ProductClientService productClientService;


    @Before
    public void setUp() {
        MockitoAnnotations.openMocks(this);
        PowerMockito.spy(MessageValidationUtils.class);
}

After i make a spy object of MessageValidationUtils.class, i was testing this:

when(MessageValidationUtils.validateTelegramKeyMap(messageProcessDto.getMessageMessageType(),
        messageProcessDto.getMessageKeyValueMap())).thenAnswer((Answer<Boolean>) invocation -> true);

After some research i couldn't find anything related to spy a class that has a private constructor and static methods.

CodePudding user response:


During mockStatic definition in Mockito you can specify setting to perform real execution by default Mockito.withSettings().defaultAnswer(Mockito.CALLS_REAL_METHODS). In this way, your static mock will work like Spy.
Let's create simple Utils class for testing.

public class Utils {
    public static String method1() {
        return "Original mehtod1() value";
    }

    public static String method2() {
        return "Original mehtod2() value";
    }

    public static String method3() {
        return method2();
    }
}

Make mock for method2 and perform real execution of method1 and method3

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

public class SpyStaticTest {
    @Test
    public void spy_static_test() {
        try (MockedStatic<Utils> utilities = Mockito.mockStatic(Utils.class, Mockito.withSettings().defaultAnswer(Mockito.CALLS_REAL_METHODS))) {
            utilities.when(Utils::method2).thenReturn("static mock");

            Assertions.assertEquals(Utils.method1(), "Original mehtod1() value");
            Assertions.assertEquals(Utils.method2(), "static mock");
            Assertions.assertEquals(Utils.method3(), "static mock");
        }
    }
}

Example for your class:

    @Test
    public void test() {
        try (MockedStatic<MessageValidationUtils> utilities = Mockito.mockStatic(MessageValidationUtils.class, Mockito.withSettings().defaultAnswer(Mockito.CALLS_REAL_METHODS))) {
            utilities.when(() -> MessageValidationUtils.validateTelegramKeyMap(messageProcessDto.getMessageMessageType(),
                    messageProcessDto.getMessageKeyValueMap())).thenAnswer((Answer<Boolean>) invocation -> true);
            
            //perform testing of your service which uses MessageValidationUtils
        }
    }
  • Related