简体   繁体   English

监视没有 PowerMock 的课程

[英]Spy a class without PowerMock

I don't want to use powermock anymore.我不想再使用 powermock 了。 Because junit5 started mocking static classes.因为 junit5 开始模拟静态类。 So i am trying to get rid of powermock methods.所以我试图摆脱 powermock 方法。

While i was using PowerMock, i could easily spy a class that has a private constructor, and then i was calling the static methods.当我使用 PowerMock 时,我可以轻松地窥探一个具有私有构造函数的类,然后调用静态方法。

This is a part of my code ( When i was using PowerMock )这是我的代码的一部分(当我使用 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:在我制作了 MessageValidationUtils.class 的间谍对象后,我正在测试这个:

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.经过一番研究,我找不到任何与监视具有私有构造函数和静态方法的类有关的东西。


During mockStatic definition in Mockito you can specify setting to perform real execution by default Mockito.withSettings().defaultAnswer(Mockito.CALLS_REAL_METHODS) .mockStatic中的 mockStatic 定义期间,您可以指定设置以默认执行实际执行Mockito.withSettings().defaultAnswer(Mockito.CALLS_REAL_METHODS) In this way, your static mock will work like Spy .这样,您的静态模拟将像Spy一样工作。
Let's create simple Utils class for testing.让我们创建简单的Utils类进行测试。

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 method3method2做mock并真正执行method1method3

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
        }
    }

UPDATE:更新:
Example to use mockStatic in @BeforeEach@BeforeEach中使用mockStatic的示例

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

public class SpyStaticTest {
    MockedStatic<Utils> utilities;
    
    @BeforeEach
    public void setUp() {
        utilities = Mockito.mockStatic(Utils.class, Mockito.withSettings().defaultAnswer(Mockito.CALLS_REAL_METHODS));
    }

    @Test
    public void spy_static_test1() {
        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");

    }

    @Test
    public void spy_static_test2() {
        utilities.when(Utils::method1).thenReturn("static mock");

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

    }

    @AfterEach
    public void  afterTest() {
        utilities.close();
    }
}

UPDATE:更新:
Mockito does not provide method for static Spy creation, but you can define own utils and implemet spy static definition there. Mockito 不提供静态Spy创建的方法,但您可以定义自己的实用程序并在那里实现 spy 静态定义。

import org.mockito.MockedStatic;
import org.mockito.Mockito;

public class MockitoUtils {
    public static <T> MockedStatic<T> spyStatic(Class<T> classToMock) {
        return Mockito.mockStatic(classToMock, Mockito.withSettings().defaultAnswer(Mockito.CALLS_REAL_METHODS));
    }
}

In this way your tests will look more clear:通过这种方式,您的测试将看起来更清晰:

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;

import static com.test.MockitoUtils.spyStatic;

public class SpyStaticTest {
    MockedStatic<Utils> utilsSpy;

    @BeforeEach
    public void setUp() {
        utilsSpy = spyStatic(Utils.class);
    }

    @Test
    public void spy_static_test1() {
        utilsSpy.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");

    }

    @Test
    public void spy_static_test2() {
        utilsSpy.when(Utils::method1).thenReturn("static mock");

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

    }

    @AfterEach
    public void  afterTest() {
        utilsSpy.close();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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