代码发布在link
现在当我正在尝试编写junit时 第一种情况我得到错误
“需要重播B级”。
但同样的junit正在为第二种情况工作。
我的junit是
@RunWith(PowerMockRunner.class)
public class TestClass {
@Test
public void testDoSomeThing() {
B b = createMock(B.class)
expectNew(b.CallMe()).andReturns(xxx)
A a=new A();
replayAll();
a.doSomething();
verifyAll();
}
}
答案 0 :(得分:1)
<强> TestClass.java 强>
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest({ A.class, B.class })
public class TestClass {
@Test
public void testDoSomeThing() throws Exception {
/* Setup */
B bMock = PowerMock.createMock(B.class);
/* Mocks */
PowerMock.expectNew(B.class).andReturn(bMock).atLeastOnce();
bMock.callMe();
/* Activate */
PowerMock.replayAll();
/* Test */
A cut = new A();
cut.doSomething();
/* Asserts */
PowerMock.verifyAll();
}
}
<强> A.java 强>
public class A {
B b = new B();
public void doSomething() {
b.callMe();
}
}
<强> B.java 强>
public class B {
public void callMe() {
}
}
答案 1 :(得分:0)
您忘了添加
@PrepareForTest({A.class, B.class})
此注释必须包含您正在模拟的类以及将使用这些模拟的类。