监视已经初始化的Java对象obsocto



我正在使用Mocktio库来编写一些测试用例,因为我具有精心的含量结构,所以我有一些在父类中实例化的对象,我想模拟其功能调用之一。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();

基本上,不要在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();
}
}

另一个示例(在这种情况下,我们用模拟替换对象 a (不需要间谍):

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();
    }
}

相关内容

  • 没有找到相关文章

最新更新