久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

      <bdo id='sIY5n'></bdo><ul id='sIY5n'></ul>
    1. <small id='sIY5n'></small><noframes id='sIY5n'>

    2. <legend id='sIY5n'><style id='sIY5n'><dir id='sIY5n'><q id='sIY5n'></q></dir></style></legend>
      <i id='sIY5n'><tr id='sIY5n'><dt id='sIY5n'><q id='sIY5n'><span id='sIY5n'><b id='sIY5n'><form id='sIY5n'><ins id='sIY5n'></ins><ul id='sIY5n'></ul><sub id='sIY5n'></sub></form><legend id='sIY5n'></legend><bdo id='sIY5n'><pre id='sIY5n'><center id='sIY5n'></center></pre></bdo></b><th id='sIY5n'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='sIY5n'><tfoot id='sIY5n'></tfoot><dl id='sIY5n'><fieldset id='sIY5n'></fieldset></dl></div>
    3. <tfoot id='sIY5n'></tfoot>

      1. TestNG + Mockito + PowerMock - verifyStatic() 不起作用

        TestNG + Mockito + PowerMock - verifyStatic() does not work(TestNG + Mockito + PowerMock - verifyStatic() 不起作用)
          <tbody id='tuurG'></tbody>
            <i id='tuurG'><tr id='tuurG'><dt id='tuurG'><q id='tuurG'><span id='tuurG'><b id='tuurG'><form id='tuurG'><ins id='tuurG'></ins><ul id='tuurG'></ul><sub id='tuurG'></sub></form><legend id='tuurG'></legend><bdo id='tuurG'><pre id='tuurG'><center id='tuurG'></center></pre></bdo></b><th id='tuurG'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='tuurG'><tfoot id='tuurG'></tfoot><dl id='tuurG'><fieldset id='tuurG'></fieldset></dl></div>
          • <tfoot id='tuurG'></tfoot>

          • <legend id='tuurG'><style id='tuurG'><dir id='tuurG'><q id='tuurG'></q></dir></style></legend>

                  <bdo id='tuurG'></bdo><ul id='tuurG'></ul>

                  <small id='tuurG'></small><noframes id='tuurG'>

                  本文介紹了TestNG + Mockito + PowerMock - verifyStatic() 不起作用的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

                  問題描述

                  我是新的 TestNG 和一般的單元測(cè)試.我將 TestNG 6.9.6 與 Mockito 1.10.19 和 PowerMock 1.6.4 一起使用.我想驗(yàn)證 MyService 類中的 myMethod() 方法是否在內(nèi)部使用正確的參數(shù)調(diào)用靜態(tài)方法 Util.myStaticMethod.由于 Mockito 本身不支持對(duì)靜態(tài)方法的驗(yàn)證,因此我將 PowerMock 與它一起使用.我的測(cè)試類如下所示:

                  公共類 MyTest{私人的我的服務(wù)我的服務(wù);@Captor ArgumentCaptor<字符串>參數(shù)捕捉器;@BeforeMethod公共無(wú)效設(shè)置(){MockitoAnnotations.initMocks( 這個(gè) );我的服務(wù) = 新的我的服務(wù)();}@測(cè)試@PrepareForTest(MyService.class)公共無(wú)效我的測(cè)試(){PowerMockito.mockStatic(Util.class);myService.myMethod("arg");PowerMockito.verifyStatic(10);Util.myStaticMethod(anyString());}}

                  這個(gè)測(cè)試預(yù)計(jì)會(huì)失敗,因?yàn)?myMethod 只調(diào)用了一次靜態(tài)方法 Util.myStaticMethod().但是當(dāng)我運(yùn)行測(cè)試時(shí),它總是通過(guò),無(wú)論我將什么值傳遞給 PowerMockito.verifyStatic().

                  另外,如果我在這個(gè)類中編寫另一個(gè)測(cè)試方法然后運(yùn)行測(cè)試,我會(huì)得到以下錯(cuò)誤

                  org.mockito.exceptions.misusing.UnfinishedVerificationException:此處缺少驗(yàn)證(模擬)的方法調(diào)用:->在 mypackage.MyTest.myTest(MyTest.java:21)正確驗(yàn)證示例:驗(yàn)證(模擬).doSomething()此外,此錯(cuò)誤可能會(huì)出現(xiàn),因?yàn)槟?yàn)證了以下任一方法:final/private/equals()/hashCode() 方法.這些方法*不能*被存根/驗(yàn)證.不支持在非公共父類上聲明的模擬方法.在 mypackage.MyTest.myTest.setup(MyTest.java:10)結(jié)果 :失敗的測(cè)試:MyTest.setup:10 未完成驗(yàn)證缺少對(duì)版本的方法調(diào)用...測(cè)試運(yùn)行:3,失敗:1,錯(cuò)誤:0,跳過(guò):1

                  它在 verifyStatic() 方法處失敗,這讓我認(rèn)為 verifyStatic() 方法需要更多我沒有提供的東西.此外,它表示測(cè)試總數(shù)為 3,而在這種情況下,我只有兩種測(cè)試方法.

                  任何幫助將不勝感激.

                  按照建議,我嘗試將 MyUtil 類放在 @PrepareForTest 注釋中,它仍然給出相同的錯(cuò)誤.

                  解決方案

                  好的,我認(rèn)為這是非常特定于 TestNG 配置的,因?yàn)樗?JUnit 示例都是開箱即用"的!

                  閱讀 PowerMock GitHub 站點(diǎn)上的 this 鏈接,該鏈接描述了有關(guān)如何將 TestNG 與 PowerMock 一起使用.使用此示例代碼在此處描述了您驗(yàn)證對(duì)模擬 static 方法的調(diào)用的場(chǎng)景:

                  <塊引用>

                  @PrepareForTest(IdGenerator.class)公共類 MyTestClass {@測(cè)試公共無(wú)效demoStaticMethodMocking()拋出異常{模擬靜態(tài)(IdGenerator.class);when(IdGenerator.generateNewId()).thenReturn(2L);新的 ClassUnderTest().methodToTest();//可選地驗(yàn)證靜態(tài)方法是否被實(shí)際調(diào)用驗(yàn)證靜態(tài)();IdGenerator.generateNewId();}}

                  然后是這個(gè)信息塊:

                  <塊引用>

                  為此,您需要告訴 TestNG 使用 PowerMock 對(duì)象工廠

                  這是使用 TestNG XML 配置或在測(cè)試代碼本身中完成的.為了完整起見,我已將上述 URL 中給出的選項(xiàng)復(fù)制到下方.FWIW 我擴(kuò)展了 PowerMockTestCase 并且驗(yàn)證按您的預(yù)期工作.

                  最后,不要忘記 @PrepareForTest 正確的類 - 即包含要模擬的 static 方法的類,正如@Bax 指出的那樣 這里.

                  作為進(jìn)一步的提示(您可能已經(jīng)知道,但無(wú)論如何這里值得一提),因?yàn)槟鷽]有使用 Mockito 來(lái)模擬對(duì)象,所以可以安全地刪除 MockitoAnnotations.initMocks(this).

                  完成所有這些工作后,您可能還想考慮是否使用 'Black Magic' 像 Powermock 這樣的工具實(shí)際上在您的設(shè)計(jì)中暴露了 代碼異味?特別是因?yàn)榭雌饋?lái)包含 static 方法的類在您的所有權(quán)之下.這意味著您可以使用不使用 static 的替代設(shè)計(jì).我強(qiáng)烈推薦 Michael Feathers 的書Working Effectively with Legacy Code,它可能會(huì)改變你的整個(gè)軟件設(shè)計(jì)和測(cè)試方法......

                  祝你好運(yùn)!

                  <塊引用>

                  配置 TestNG 以使用 PowerMock 對(duì)象工廠

                  使用套件.xml

                  在您的 suite.xml 中,在套件標(biāo)簽中添加以下內(nèi)容:object-factory="org.powermock.modules.testng.PowerMockObjectFactory" 例如

                  <套件名稱="dgf" verbose="10"object-factory="org.powermock.modules.testng.PowerMockObjectFactory"><測(cè)試名稱="dgf"><類><類名="com.mycompany.Test1"/><類名="com.mycompany.Test2"/></類></測(cè)試>

                  如果您使用的是 Maven,您可能需要向 Surefire 指出該文件:

                  <插件><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><配置><suiteXmlFiles><suiteXmlFile>suite.xml</suiteXmlFile></suiteXmlFiles></配置></插件>

                  以編程方式

                  將這樣的方法添加到您的測(cè)試類中:

                  @ObjectFactory public IObjectFactory getObjectFactory() {返回新的 org.powermock.modules.testng.PowerMockObjectFactory();}

                  或者為了安全起見,您也可以從 PowerMockTestCase 進(jìn)行擴(kuò)展:

                  @PrepareForTest(IdGenerator.class)公共類 MyTestClass 擴(kuò)展 PowerMockTestCase { ... }

                  I am new TestNG and unit-testing in general. I am using TestNG 6.9.6 with Mockito 1.10.19 and PowerMock 1.6.4. I want to verify whether the myMethod() method in MyService class internally calls the static method Util.myStaticMethod with the correct arguments. Since verification of static methods is not natively supported in Mockito, I am using PowerMock along with it. My Test class is shown below:

                  public class MyTest
                  {
                      private MyService myService;
                  
                      @Captor ArgumentCaptor<String> argCaptor;
                  
                      @BeforeMethod
                      public void setup()
                      {
                          MockitoAnnotations.initMocks( this );
                          myService = new MyService();
                      }
                  
                      @Test
                      @PrepareForTest(MyService.class)
                      public void myTest()
                      {
                          PowerMockito.mockStatic(Util.class);
                          myService.myMethod("arg");
                  
                          PowerMockito.verifyStatic(10);
                          Util.myStaticMethod(anyString());
                      }
                  }
                  

                  This test is expected to fail, as myMethod calls the static method Util.myStaticMethod() only once. But when i run the test, it always passes, no matter what value i pass to PowerMockito.verifyStatic().

                  Also, if I write another test method in this class and then run the test, I get the following error

                  org.mockito.exceptions.misusing.UnfinishedVerificationException: 
                  Missing method call for verify(mock) here:
                  -> at mypackage.MyTest.myTest(MyTest.java:21)
                  
                  Example of correct verification:
                      verify(mock).doSomething()
                  
                  Also, this error might show up because you verify either of: final/private/equals()/hashCode() methods.
                  Those methods *cannot* be stubbed/verified.
                  Mocking methods declared on non-public parent classes is not supported.
                  
                      at mypackage.MyTest.myTest.setup(MyTest.java:10)
                  
                  
                  Results :
                  
                  Failed tests: 
                    MyTest.setup:10 UnfinishedVerification 
                  Missing method call for ver...
                  
                  Tests run: 3, Failures: 1, Errors: 0, Skipped: 1
                  

                  It fails at the verifyStatic() method, which makes me think that the verifyStatic() method needs something more that i am not providing. Also, it indicates the total number of tests as 3 whereas in this case I have only two test methods.

                  Any help would be appreciated.

                  EDIT : As suggested, I tried putting MyUtil class in the the @PrepareForTest annotation, it still gives the same error.

                  解決方案

                  OK, I think this is very specific to TestNG configurations, since all of the JUnit examples work 'out of the box'!

                  Read through this link from the PowerMock GitHub site which describes further detail on how to use TestNG together with PowerMock. Exactly your scenario of verifying calls to mocked static methods is described there using this example code:

                  @PrepareForTest(IdGenerator.class)
                  public class MyTestClass {
                  
                     @Test
                     public void demoStaticMethodMocking() throws Exception {
                        mockStatic(IdGenerator.class);
                        when(IdGenerator.generateNewId()).thenReturn(2L);       
                        new ClassUnderTest().methodToTest();
                  
                        // Optionally verify that the static method was actually called
                        verifyStatic();
                        IdGenerator.generateNewId();
                     }
                  
                  }
                  

                  Which is then followed by this nugget of information:

                  For this to work you need to tell TestNG to use the PowerMock object factory

                  This is done using either TestNG XML config, or in the test's code itself. For completeness I've copied below the options given at the above URL. FWIW I extended PowerMockTestCase and the verification worked as you expect.

                  Finally, don't forget to @PrepareForTest the correct class - i.e. the class containing the static methods which you want to mock, as @Bax pointed out here.

                  As a further hint (which you probably already know about, but worth mentioning here anyway) since you aren't using Mockito to mock objects, MockitoAnnotations.initMocks(this) can be safely deleted.

                  Once you've got all this working, you might also like to consider whether the use of 'Black Magic' tools like Powermock is actually exposing code smells in your design? Especially since it looks like the classes containing static methods are under your ownership. This means you could use an alternative design that doesn't use statics. I highly recommend Michael Feathers' book Working Effectively with Legacy Code, it might just change your whole approach to software design and testing...

                  Good luck!

                  Configure TestNG to use the PowerMock object factory

                  Using suite.xml

                  In your suite.xml add the following in the suite tag: object-factory="org.powermock.modules.testng.PowerMockObjectFactory" e.g.

                  <suite name="dgf" verbose="10"
                      object-factory="org.powermock.modules.testng.PowerMockObjectFactory">
                      <test name="dgf">
                          <classes>
                              <class name="com.mycompany.Test1"/>
                              <class name="com.mycompany.Test2"/>
                          </classes>
                      </test> 
                  

                  If you're using Maven you may need to point out the file to Surefire:

                  <plugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-surefire-plugin</artifactId>
                      <configuration>
                          <suiteXmlFiles>
                          <suiteXmlFile>suite.xml</suiteXmlFile>
                          </suiteXmlFiles>
                      </configuration> 
                  </plugin> 
                  

                  Programmatically

                  Add a method like this to your test class:

                  @ObjectFactory public IObjectFactory getObjectFactory() {
                       return new org.powermock.modules.testng.PowerMockObjectFactory(); 
                  } 
                  

                  or to be on the safe side you can also extend from the PowerMockTestCase:

                  @PrepareForTest(IdGenerator.class) 
                  public class MyTestClass extends PowerMockTestCase { ... }
                  

                  這篇關(guān)于TestNG + Mockito + PowerMock - verifyStatic() 不起作用的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  How can I detect integer overflow on 32 bits int?(如何檢測(cè) 32 位 int 上的整數(shù)溢出?)
                  Local variables before return statements, does it matter?(return 語(yǔ)句之前的局部變量,這有關(guān)系嗎?)
                  How to convert Integer to int?(如何將整數(shù)轉(zhuǎn)換為整數(shù)?)
                  How do I create an int array with randomly shuffled numbers in a given range(如何在給定范圍內(nèi)創(chuàng)建一個(gè)隨機(jī)打亂數(shù)字的 int 數(shù)組)
                  Inconsistent behavior on java#39;s ==(java的行為不一致==)
                  Why is Java able to store 0xff000000 as an int?(為什么 Java 能夠?qū)?0xff000000 存儲(chǔ)為 int?)
                  <i id='y3mpH'><tr id='y3mpH'><dt id='y3mpH'><q id='y3mpH'><span id='y3mpH'><b id='y3mpH'><form id='y3mpH'><ins id='y3mpH'></ins><ul id='y3mpH'></ul><sub id='y3mpH'></sub></form><legend id='y3mpH'></legend><bdo id='y3mpH'><pre id='y3mpH'><center id='y3mpH'></center></pre></bdo></b><th id='y3mpH'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='y3mpH'><tfoot id='y3mpH'></tfoot><dl id='y3mpH'><fieldset id='y3mpH'></fieldset></dl></div>

                    <bdo id='y3mpH'></bdo><ul id='y3mpH'></ul>
                      <tfoot id='y3mpH'></tfoot>
                      1. <small id='y3mpH'></small><noframes id='y3mpH'>

                            <tbody id='y3mpH'></tbody>
                          • <legend id='y3mpH'><style id='y3mpH'><dir id='y3mpH'><q id='y3mpH'></q></dir></style></legend>

                            主站蜘蛛池模板: 91操操操| 第四色播日韩第一页 | 欧美精品在线免费观看 | 亚洲精品一区国产精品 | 国产精品久久久久久久久久久久 | 特级黄一级播放 | 一区二区三区视频在线观看 | 亚洲一区在线免费观看 | 国产福利精品一区 | 精品国产18久久久久久二百 | 免费黄色a级毛片 | 日本免费在线 | 国产精品久久久久久久久免费樱桃 | 亚洲成人动漫在线观看 | 精品亚洲永久免费精品 | 一区二区三区精品 | 中文字幕在线一区二区三区 | 国产激情在线播放 | 国产成人在线一区二区 | 超碰成人免费观看 | 一区在线视频 | 亚洲九色| 精品不卡 | 色99视频 | 在线中文视频 | 天天草天天爱 | 欧美精品一区二区在线观看 | 国产精品色哟哟网站 | 夜夜夜久久久 | 精品国产伦一区二区三区观看方式 | 欧美在线一区二区三区四区 | 国产欧美在线 | a在线视频| av黄色在线 | 一级片aaa| 中文字幕视频一区 | av片在线免费看 | 亚洲精品美女在线观看 | 亚洲精品福利在线 | 亚洲色图综合网 | 在线a视频 |