我试图使用Power Mock模拟某些场景。
我在以下测试方法上遇到断言错误 - returnSevenTest()
。
要测试的类
package tutorial.one;
public class TutorialOne {
//fields
private int number;
private String name;
private final DependentFinalClass finalObj;
public TutorialOne (DependentFinalClass dep) {
this.finalObj= dep ;
}
public TutorialOne(int number, String name) {
super();
this.number = number;
this.name = name;
finalObj = new DependentFinalClass();
}
public static int returnTwo()
{
return 2;
}
public int returnThree()
{
return (returnTwo()+1);
}
public final int returnFour()
{
return (returnThree()+1);
}
private final int returnFive(int addOne)
{
return (returnFour()+addOne);
}
public int returnSix(int addOne)
{
return (returnFive(1)+addOne);
}
public int returnSeven()
{
final int addValue= finalObj.callMeToAddOne(true);
return (returnSix(1)+ addValue);
}
}
最终成绩
package tutorial.one;
public final class DependentFinalClass {
final int callMeToAddOne(Boolean need)
{
if(need.equals(true))
return 1;
else
return 1;
}
}
使用Power Mock + Easy Mock测试表演课程
package tutorial.one;
import stmts ....
@RunWith(PowerMockRunner.class)
//We prepare the IdGenerator for test because the static method is normally not
//mockable
@PrepareForTest({TutorialOne.class, DependentFinalClass.class}) public class TutorialOneTests {
@Test
public void returnSevenTest() throws Exception
{
int expected=7;
TutorialOne privateTest =createPartialMock(TutorialOne.class,"returnFive" );
DependentFinalClass finalDependentTest = createPartialMock(DependentFinalClass.class,"callMeToAddOne" );
expect(finalDependentTest.callMeToAddOne(true)).andReturn(1);
expectNew(TutorialOne.class, finalDependentTest).andReturn(privateTest);
expectPrivate(privateTest, "returnFive", 1).andReturn(5);
replayAll(TutorialOne.class,finalDependentTest,privateTest);
int actual = privateTest.returnSeven();
verifyAll();
assertEquals("Expected and actual did not match", expected, actual);
resetAll();
}
}
收到错误:
java.lang.AssertionError:
Expectation failure on verify:
tutorial.one.TutorialOne(tutorial.one.DependentFinalClass$$EnhancerByCGLIB$$fa2e41a0@5a9de6): expected: 1, actual: 0
at org.powermock.api.easymock.internal.invocationcontrol.NewInvocationControlAssertionError.throwAssertionErrorForNewSubstitutionFailure(NewInvocationControlAssertionError.java:20)
at org.powermock.api.easymock.PowerMock.verifyClass(PowerMock.java:2279)
at org.powermock.api.easymock.PowerMock.verify(PowerMock.java:1646)
at org.powermock.api.easymock.PowerMock.verifyAll(PowerMock.java:1586)
at tutorial.one.TutorialOneTests.returnSevenTest(TutorialOneTests.java:174)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
任何帮助都非常感谢!!感谢
答案 0 :(得分:0)
我认为问题是你已经宣布了期望: expectNew(TutorialOne.class,finalDependentTest).andReturn(privateTest); 但是在你的方法调用它没有被调用,尝试在返回后添加anyTimes(): expectNew(TutorialOne.class,finalDependentTest).andReturn(privateTest).anyTimes(); 或删除此期望,因为您在测试中不使用构造函数,只使用部分模拟实例。