假设我有一个非最终具体类,它具有如下所示的final方法。publicclassABC{publicfinalStringmyMethod(){return"testtest";}}是否可以模拟myMethod()在junit中使用Powermockito调用时返回其他内容?谢谢 最佳答案 这个有效:@RunWith(PowerMockRunner.class)@PrepareForTest(ABC.class)publicclassABCTest{@TestpublicvoidfinalCouldBeMock(){finalABC
我正在尝试模拟私有(private)静态方法anotherMethod()。看下面的代码publicclassUtil{publicstaticStringmethod(){returnanotherMethod();}privatestaticStringanotherMethod(){thrownewRuntimeException();//logicwasreplacedwithexception.}}这是我的测试代码@PrepareForTest(Util.class)publicclassUtilTestextendsPowerMockTestCase{@Testpublic
我有一个类似下面的代码:ClassA{publicbooleanmyMethod(someargs){MyQueryClassquery=newMyQueryClass();Longid=query.getNextId();//somemorecode}}ClassMyQueryClass{....publicLonggetNextId(){//lotofDBcode,executesomeDBqueryreturnid;}}现在我正在为A.myMethod(someargs)编写测试。我想跳过真正的方法query.getNextId()而是返回一个stub值。基本上,我想模拟MyQu
我有一个类似下面的代码:ClassA{publicbooleanmyMethod(someargs){MyQueryClassquery=newMyQueryClass();Longid=query.getNextId();//somemorecode}}ClassMyQueryClass{....publicLonggetNextId(){//lotofDBcode,executesomeDBqueryreturnid;}}现在我正在为A.myMethod(someargs)编写测试。我想跳过真正的方法query.getNextId()而是返回一个stub值。基本上,我想模拟MyQu