Powermock 和 Mockito - 模拟被忽略
Powermock and Mockito - mock is ignored
我正在尝试向我的 Main.class 中注入子模拟,但它似乎不起作用。
(将 powermock 1.7.0 与依赖项捆绑在一起的 junit 一起使用)
验证表明我的模拟对象没有交互。想不通为什么。
这是我的 Main.class:
public class Main {
private Child child;
public Main(){ }
public void setChild(Child child){
this.child = child;
}
public void play(){
child = new Child();
child.setNumber(50);
System.out.println(child.getNumber());
}
}
这是我的 Child.class :
public class Child {
private int number;
public void setNumber(int number){
this.number = number;
}
public int getNumber(){
return number;
}
}
这是我的 Test.class:
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.when;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest({Child.class, Main.class})
public class MainTest {
private Child child;
private Main main;
@Test
public void testMain(){
main = new Main();
main.play();
Mockito.verify(child).getNumber();
}
@Before
public void setup(){
child = mock(Child.class);
when(child.getNumber()).thenReturn(10);
}
}
您在测试中创建的 mock 从未实际使用过,因为 Main 对象会在您每次对其调用 play() 时创建一个新的 Child 对象。
你想要的是一种告诉生产代码使用模拟子实例的方法,例如通过 setter.
主要
public void play(){
// child = new Child(); // do not create a new instance each time
child.setNumber(50);
System.out.println(child.getNumber());
}
主测试
@Test
public void testMain(){
main = new Main();
main.setChild(child); // tell it to use the mock
main.play();
Mockito.verify(child).getNumber();
}
我正在尝试向我的 Main.class 中注入子模拟,但它似乎不起作用。 (将 powermock 1.7.0 与依赖项捆绑在一起的 junit 一起使用)
验证表明我的模拟对象没有交互。想不通为什么。
这是我的 Main.class:
public class Main {
private Child child;
public Main(){ }
public void setChild(Child child){
this.child = child;
}
public void play(){
child = new Child();
child.setNumber(50);
System.out.println(child.getNumber());
}
}
这是我的 Child.class :
public class Child {
private int number;
public void setNumber(int number){
this.number = number;
}
public int getNumber(){
return number;
}
}
这是我的 Test.class:
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.when;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest({Child.class, Main.class})
public class MainTest {
private Child child;
private Main main;
@Test
public void testMain(){
main = new Main();
main.play();
Mockito.verify(child).getNumber();
}
@Before
public void setup(){
child = mock(Child.class);
when(child.getNumber()).thenReturn(10);
}
}
您在测试中创建的 mock 从未实际使用过,因为 Main 对象会在您每次对其调用 play() 时创建一个新的 Child 对象。
你想要的是一种告诉生产代码使用模拟子实例的方法,例如通过 setter.
主要
public void play(){
// child = new Child(); // do not create a new instance each time
child.setNumber(50);
System.out.println(child.getNumber());
}
主测试
@Test
public void testMain(){
main = new Main();
main.setChild(child); // tell it to use the mock
main.play();
Mockito.verify(child).getNumber();
}