• <small id='WehgX'></small><noframes id='WehgX'>

  • <i id='WehgX'><tr id='WehgX'><dt id='WehgX'><q id='WehgX'><span id='WehgX'><b id='WehgX'><form id='WehgX'><ins id='WehgX'></ins><ul id='WehgX'></ul><sub id='WehgX'></sub></form><legend id='WehgX'></legend><bdo id='WehgX'><pre id='WehgX'><center id='WehgX'></center></pre></bdo></b><th id='WehgX'></th></span></q></dt></tr></i><div id='WehgX'><tfoot id='WehgX'></tfoot><dl id='WehgX'><fieldset id='WehgX'></fieldset></dl></div>

    1. <legend id='WehgX'><style id='WehgX'><dir id='WehgX'><q id='WehgX'></q></dir></style></legend>
      <tfoot id='WehgX'></tfoot>

          <bdo id='WehgX'></bdo><ul id='WehgX'></ul>
      1. 即使在添加 @PrepareForTest 之后,Mockito 也会要求为

        时间:2023-09-25
        • <small id='r0XFy'></small><noframes id='r0XFy'>

            <legend id='r0XFy'><style id='r0XFy'><dir id='r0XFy'><q id='r0XFy'></q></dir></style></legend>
              • <bdo id='r0XFy'></bdo><ul id='r0XFy'></ul>
                <i id='r0XFy'><tr id='r0XFy'><dt id='r0XFy'><q id='r0XFy'><span id='r0XFy'><b id='r0XFy'><form id='r0XFy'><ins id='r0XFy'></ins><ul id='r0XFy'></ul><sub id='r0XFy'></sub></form><legend id='r0XFy'></legend><bdo id='r0XFy'><pre id='r0XFy'><center id='r0XFy'></center></pre></bdo></b><th id='r0XFy'></th></span></q></dt></tr></i><div id='r0XFy'><tfoot id='r0XFy'></tfoot><dl id='r0XFy'><fieldset id='r0XFy'></fieldset></dl></div>

                    <tbody id='r0XFy'></tbody>

                1. <tfoot id='r0XFy'></tfoot>
                2. 本文介绍了即使在添加 @PrepareForTest 之后,Mockito 也会要求为该类添加 @PrepareForTest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有以下简单的代码.我有一个类(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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:每次测试都会重置 Mock 对象吗? 下一篇:使用 Mockito 模拟接口

                  相关文章

                  最新文章

                      <bdo id='FJjCx'></bdo><ul id='FJjCx'></ul>
                  1. <tfoot id='FJjCx'></tfoot>
                    <legend id='FJjCx'><style id='FJjCx'><dir id='FJjCx'><q id='FJjCx'></q></dir></style></legend>
                  2. <small id='FJjCx'></small><noframes id='FJjCx'>

                      <i id='FJjCx'><tr id='FJjCx'><dt id='FJjCx'><q id='FJjCx'><span id='FJjCx'><b id='FJjCx'><form id='FJjCx'><ins id='FJjCx'></ins><ul id='FJjCx'></ul><sub id='FJjCx'></sub></form><legend id='FJjCx'></legend><bdo id='FJjCx'><pre id='FJjCx'><center id='FJjCx'></center></pre></bdo></b><th id='FJjCx'></th></span></q></dt></tr></i><div id='FJjCx'><tfoot id='FJjCx'></tfoot><dl id='FJjCx'><fieldset id='FJjCx'></fieldset></dl></div>