pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

      <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="l7nzr7z" 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="tjr7bp7" 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à)值,需要的朋友們下面隨著小編來一起學(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公共無效設(shè)置(){MockitoAnnotations.initMocks( 這個(gè) );我的服務(wù) = 新的我的服務(wù)();}@測(cè)試@PrepareForTest(MyService.class)公共無效我的測(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í),它總是通過,無論我將什么值傳遞給 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,跳過: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è)試公共無效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)知道,但無論如何這里值得一提),因?yàn)槟鷽]有使用 Mockito 來模擬對(duì)象,所以可以安全地刪除 MockitoAnnotations.initMocks(this).

                  完成所有這些工作后,您可能還想考慮是否使用 'Black Magic' 像 Powermock 這樣的工具實(shí)際上在您的設(shè)計(jì)中暴露了 代碼異味?特別是因?yàn)榭雌饋戆?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)容來源于互聯(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="bbzxtht" 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>

                            主站蜘蛛池模板: 电抗器-能曼电气-电抗器专业制造商 | 恒温恒湿箱(药品/保健品/食品/半导体/细菌)-兰贝石(北京)科技有限公司 | 比亚迪叉车-比亚迪电动叉车堆垛车托盘车仓储叉车价格多少钱报价 磁力去毛刺机_去毛刺磁力抛光机_磁力光饰机_磁力滚抛机_精密金属零件去毛刺机厂家-冠古科技 | 上海皓越真空设备有限公司官网-真空炉-真空热压烧结炉-sps放电等离子烧结炉 | 萃取箱-萃取槽-PVC萃取箱厂家-混合澄清槽- 杭州南方化工设备 | 轴承振动测量仪电箱-轴承测振动仪器-测试仪厂家-杭州居易电气 | 解放卡车|出口|济南重汽|报价大全|山东三维商贸有限公司 | 深圳VI设计-画册设计-LOGO设计-包装设计-品牌策划公司-[智睿画册设计公司] | 电镀标牌_电铸标牌_金属标贴_不锈钢标牌厂家_深圳市宝利丰精密科技有限公司 | 氧化锆纤维_1800度高温退火炉_1800度高温烧结炉-南京理工宇龙新材料股份有限公司 | 带式压滤机_污泥压滤机_污泥脱水机_带式过滤机_带式压滤机厂家-河南恒磊环保设备有限公司 | 山西3A认证|太原AAA信用认证|投标AAA信用证书-山西AAA企业信用评级网 | 电解抛光加工_不锈钢电解抛光_常州安谱金属制品有限公司 | 执业药师报名时间,报考条件,考试时间-首页入口 | 石家庄网站建设|石家庄网站制作|石家庄小程序开发|石家庄微信开发|网站建设公司|网站制作公司|微信小程序开发|手机APP开发|软件开发 | 全自动烧卖机厂家_饺子机_烧麦机价格_小笼汤包机_宁波江北阜欣食品机械有限公司 | 体视显微镜_荧光生物显微镜_显微镜报价-微仪光电生命科学显微镜有限公司 | 药品仓库用除湿机-变电站用防爆空调-油漆房用防爆空调-杭州特奥环保科技有限公司 | 石家庄救护车出租_重症转院_跨省跨境医疗转送_活动赛事医疗保障_康复出院_放弃治疗_腾康26年医疗护送转诊团队 | 苹果售后维修点查询,苹果iPhone授权售后维修服务中心 – 修果网 拼装地板,悬浮地板厂家,悬浮式拼装运动地板-石家庄博超地板科技有限公司 | 冷柜风机-冰柜电机-罩极电机-外转子风机-EC直流电机厂家-杭州金久电器有限公司 | 建大仁科-温湿度变送器|温湿度传感器|温湿度记录仪_厂家_价格-山东仁科 | 小程序开发公司_APP开发多少钱_软件开发定制_微信小程序制作_客户销售管理软件-济南小溪畅流网络科技有限公司 | 塑料检查井_双扣聚氯乙烯增强管_双壁波纹管-河南中盈塑料制品有限公司 | 无压烧结银_有压烧结银_导电银胶_导电油墨_导电胶-善仁(浙江)新材料 | 尾轮组_头轮组_矿用刮板_厢式刮板机_铸石刮板机厂家-双驰机械 | 微信聊天记录恢复_手机短信删除怎么恢复_通讯录恢复软件下载-快易数据恢复 | 篮球架_乒乓球台_足球门_校园_竞技体育器材_厂家_价格-沧州浩然体育器材有限公司 | 有源电力滤波装置-电力有源滤波器-低压穿排电流互感器|安科瑞 | CCE素质教育博览会 | CCE素博会 | 教育展 | 美育展 | 科教展 | 素质教育展 | 扬州汇丰仪表有限公司 | [品牌官网]贵州遵义双宁口腔连锁_贵州遵义牙科医院哪家好_种植牙_牙齿矫正_原华美口腔 | 气力输送_输送机械_自动化配料系统_负压吸送_制造主力军江苏高达智能装备有限公司! | 电销卡 防封电销卡 不封号电销卡 电话销售卡 白名单电销卡 电销系统 外呼系统 | 曙光腾达官网-天津脚手架租赁-木板架出租-移动门式脚手架租赁「免费搭设」 | 镀锌方管,无缝方管,伸缩套管,方矩管_山东重鑫致胜金属制品有限公司 | 物和码官网,物和码,免费一物一码数字化营销SaaS平台 | 减速机三参数组合探头|TSM803|壁挂式氧化锆分析仪探头-安徽鹏宸电气有限公司 | CE认证_产品欧盟ROHS-REACH检测机构-商通检测 | 大巴租车平台承接包车,通勤班车,巴士租赁业务 - 鸿鸣巴士 | 继电器模组-IO端子台-plc连接线-省配线模组厂家-世麦德 |