Mockito mock 静态 void 方法

        MockedStatic<SpringContextUtils> utils = Mockito.mockStatic(SpringContextUtils.class);
        utils.when(() ->SpringContextUtils.publishEvent(any())).thenAnswer(invocation -> null);
...
//最后需要关闭
utils.close();

maven

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>3.7.7</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-inline</artifactId>
            <version>3.7.7</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-junit-jupiter</artifactId>
            <version>3.7.7</version>
            <scope>test</scope>
        </dependency>
posted @ 2023-02-22 17:42  大哥超帅  阅读(2902)  评论(1编辑  收藏  举报