监视没有PowerMock的课程

发布于 2025-01-31 04:47:27 字数 909 浏览 3 评论 0原文

我不想再使用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);
}

是MessageValidationUtilsss.class的间谍对象后,我正在测试以下内容:

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

经过一些研究,我找不到与有私人构造师和私人构造师和的班级有关的东西静态方法。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耳钉梦 2025-02-07 04:47:27

MockStatic在Mockito中的定义您可以指定设置以默认情况下执行真实执行mockito.withSettings()。defaultanswer(mockito.calls_real_methods)。这样,您的静态模拟将像间谍一样工作。

让我们创建简单的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
        }
    }

更新:

使用模拟> in @beforeeach

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不提供静态间谍创建的方法,但是您可以在那里定义自己的UTILS和IMPLEMET间谍静态定义。

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

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

UPDATE:

Example to use mockStatic in @BeforeEach

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.

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文