mockito 在没有PowerMock的情况下监视类

bhmjp9jg  于 2022-11-08  发布在  其他
关注(0)|答案(1)|浏览(138)

我不想再用powermock了。因为junit5开始嘲笑静态类了。所以我试着去掉powermock方法。
当我使用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);
}

在我创建MessageValidationUtils.class的间谍对象后,我测试了以下内容:

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

经过一些研究,我找不到任何与间谍类,有一个私有构造函数和静态方法。

gojuced7

gojuced71#

在Mockito中定义mockStatic时,您可以指定默认执行Mockito.withSettings().defaultAnswer(Mockito.CALLS_REAL_METHODS)的设置。这样,您的静态模拟将像Spy一样工作。
让我们创建一个简单的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();
    }
}

method2进行模拟,并执行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");
        }
    }
}

您的类示例:

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

更新日期:

@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();
    }
}

更新日期:

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

这样,您的测试将看起来更清晰:

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();
    }
}

相关问题