即使在添加 @PrepareForTest 之后,Mockito 也会要求为该类添加 @PrepareForTest

Mockito asks to add @PrepareForTest for the class even after adding @PrepareForTest(即使在添加 @PrepareForTest 之后,Mockito 也会要求为该类添加 @PrepareForTest)
本文介绍了即使在添加 @PrepareForTest 之后,Mockito 也会要求为该类添加 @PrepareForTest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有以下简单的代码.我有一个类(TestClass),我想测试someMethod".我的someMethod"调用了一个外部静态方法.我想对该静态方法进行 Powermock 以返回一些虚拟对象.我一开始就有@PrepareForTest(ExternalClass.class),但是当我执行它时会出现错误:

I have the following simple code. I have a class (TestClass) and I want to test "someMethod". There is an external static method which is called by my "someMethod". I want to Powermock that static method to return me some dummy object. I have the @PrepareForTest(ExternalClass.class) in the begining, but when I execute it gives the error:

ExternalClass 类没有准备好进行测试.要准备此类,请将类添加到 '@PrepareForTest' 注释.如果您不使用此注解,请在类或方法级别添加注解.

The class ExternalClass not prepared for test. To prepare this class, add class to the '@PrepareForTest' annotation. In case if you don't use this annotation, add the annotation on class or method level.

请帮我指出我使用 @PrepareForTest

@RunWith(PowerMockRunner.class)
@PrepareForTest(ExternalClass.class)
public class xyzTest {  
    @Mock
    private RestTemplate restTemplate;

    @Mock
    private TestClass testClass;

    @BeforeClass
    private void setUpBeforeClass() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void testSuccessCase() {
        Boolean mockResponse = true;
        ResponseEntity<Boolean> response = new ResponseEntity<Boolean>(mockResponse, HttpStatus.OK);
        SomeClass someClass = new SomeClass("test", "1.0.0", "someUrl", "someMetaData");

        PowerMockito.mockStatic(ExternalClass.class);

        Mockito.when(restTemplate.postForEntity(any(String.class), any(String.class), eq(Boolean.class))).thenReturn(response);
        Mockito.when(ExternalClass.getSomeClass(any(String.class))).thenReturn(someClass);

        Boolean result = testClass.someMethod("test");

        Assert.isTrue(result);
        Mockito.verify(restTemplate, times(1)).postForObject(any(String.class), any(String.class), any());
    }
}

推荐答案

确保将 @RunWith(PowerMockRunner.class) 也添加到类的顶部.

Make sure you add @RunWith(PowerMockRunner.class) to the top of your class as well.

::edit:: 两年后...

永远不要使用 PowerMockito,你不应该这样做.

Don't ever use PowerMockito, you shouldn't need to.

如果您确实需要,您很可能违反了 SOLID 原则并且您的设计是错误的.

If you do need to, you have most likely broken the SOLID principles and your design is wrong.

改正你的设计.

这篇关于即使在添加 @PrepareForTest 之后,Mockito 也会要求为该类添加 @PrepareForTest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Sending a keyboard event from java to any application (on-screen-keyboard)(将键盘事件从 java 发送到任何应用程序(屏幕键盘))
How to make JComboBox selected item not changed when scrolling through its popuplist using keyboard(使用键盘滚动其弹出列表时如何使 JComboBox 所选项目不更改)
Capturing keystrokes without focus(在没有焦点的情况下捕获击键)
How can I position a layout right above the android on-screen keyboard?(如何将布局放置在 android 屏幕键盘的正上方?)
How to check for key being held down on startup in Java(如何检查在Java中启动时按住的键)
Android - Get keyboard key press(Android - 获取键盘按键)