我正在使用Mocktio库来编写一些测试用例,因为我有一个精心设计的inhertance结构,我有几个在父类中实例化的对象,我想模拟它的一个函数调用。 Mockito库是否提供了监视已初始化对象的任何方法?
此外,该对象不能直接实例化。
类似于以下内容 -
public class A {
protected static MyObject a;
public static void someMethod() {
a = myObjectBuilder.createObj();
}
}
另一个B类看起来类似于
class B extends A {
@BeforeClass
public static void setUpBeforeClass() {
someMethod();
}
@Test
public void mockTest() {
// now mock behavior of some method of MyObject a
// Missing line to spy object a.
Mockito.doReturn(false).when(a).xyz();
/* Now call some method that triggers a.xyz()
again, it is not a direct call,
there are multiple layer of abstraction
*/
}
}
编辑:我已尝试过以下内容但无效
MyObject mock_object = Mockito.spy(a);
Mockito.doReturn(false).when(mock_object).xyz();
答案 0 :(得分:0)
基本上,不要在BeforeClass中进行初始化,它只运行一次但是 你需要在每次测试中都有新的间谍,或者你必须“重新初始化”间谍对象 在每次测试之前。 请检查此代码:
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.Mockito;
import static org.assertj.core.api.Assertions.assertThat;
class MyObject{
public String cos;
public MyObject(String cos) {
this.cos = cos;
}
public boolean xyz() {
return true;
}
}
class A {
protected static MyObject a;
public void someMethod() {
a = new MyObject("cccc");
}
}
public class B extends A {
@Before
public void setUpBeforeTest() {
someMethod();
}
@Test
public void mockTest() {
MyObject mock_object = Mockito.spy(a);
Mockito.doReturn(false).when(mock_object).xyz();
assertThat(mock_object.xyz()).isFalse();
}
@Test
public void mockTest2() {
MyObject mock_object = Mockito.spy(a);
Mockito.doReturn(true).when(mock_object).xyz();
assertThat(mock_object.xyz()).isTrue();
}
}
如果您想按自己的方式行事,请更改:
public void someMethod() {
a = myObjectBuilder.createObj();
}
成:
public static void someMethod() {
a = myObjectBuilder.createObj();
}
您无法从静态初始化程序@BeforeClass调用非静态方法:
class A {
protected static MyObject a;
public static void someMethod() {
a = new MyObject("cccc");
}
}
public class B extends A {
@BeforeClass
public static void setUpBeforeClass() {
someMethod();
}
@Test
public void mockTest() {
MyObject mock_object = Mockito.spy(a);
Mockito.doReturn(false).when(mock_object).xyz();
assertThat(mock_object.xyz()).isFalse();
}
@Test
public void mockTest2() {
MyObject mock_object = Mockito.spy(a);
// Here we replace original object with our spy
A.a = mock_object;
Mockito.doReturn(false).when(mock_object).xyz();
assertThat(a.xyz()).isFalse();
}
}
另一个例子(在这种情况下,我们用mock替换对象 a (不需要spy):
class MyObject{
public String cos;
public MyObject(String cos) {
this.cos = cos;
}
public boolean xyz() {
return true;
}
}
class A {
protected MyObject a;
public A() {
a = new MyObject("ggggg");
}
public String doSomethingWithA(){
if(a.xyz()){
return a.cos;
}
else{
return "aaaa";
}
}
}
@RunWith(MockitoJUnitRunner.class)
public class B {
@Mock
MyObject mock_object;
@InjectMocks
A systemUnderTest = new A();
@Test
public void mockTest1() {
Mockito.doReturn(false).when(mock_object).xyz();
assertThat(systemUnderTest.doSomethingWithA()).isEqualTo("aaaa");
}
@Test
public void mockTest2() {
Mockito.doReturn(true).when(mock_object).xyz();
assertThat(systemUnderTest.doSomethingWithA()).isNull();
}
}