问题描述
大家好:我在做单元测试的时候,遇到了调用其他模块的一个单例的方法,想用PowerMockmock一下这个类,预计单例类方法的返回至,但一直不成功,请各位大神帮忙看一下:单例类:publicclassDealData{privateDealData(){}publicstaticDealDatainstance=newDealData();publicstaticDealDatagetInstance(){returninstance;}publicStringdeal(Stringinput){returnnull;}}业务调用类:publicclassDealService{publicStringdealData(Stringinput){returninput+DealData.getInstance().deal("OK");}}测试类:@TestpublicvoidtestDealData1(){DealDatadealData=PowerMock.createMock(DealData.class);EasyMock.expect(dealData.deal("OK")).andReturn("world");PowerMock.replayAll();DealServicedealService=newDealService();Stringresult=dealService.dealData("Hello");assertEquals("HelloWorld",result);}无论怎么Mock,都说单例类的deal方法返回为null请大家帮忙看一下,谢谢
解决方案
时间: 2024-12-24 20:31:29