简体   繁体   English

如何在mockito中模拟Spring依赖项

[英]How to mock Spring dependencies in mockito

I am trying to mock Spring Beans. 我想嘲笑春豆。 I am able to Mock object B and C. But am not able to Mock the object inside class B. The mock that is inserted in class A contains B . 我能够模拟对象B和C.但是我不能在类B中模拟对象。在类A中插入的模拟包含B. but X and Y are null even though i have mocked them. 但是即使我嘲笑过它们,X和Y也是空的。 is there any way in Mockito to mock the Objects of member of member inside the Spring bean. 在Mockito中有什么方法可以模拟Spring bean中成员成员的对象。

@Named
@Scope(value = "prototype")
public class A {        
    @Inject
    private B b;
    @Inject
    private C c;        
}

@Named
@Scope(value = "prototype")
public class B {        
    @Inject
    private X x;
    @Inject
    private Y y;        
}

The Testing Class in which i need to populate all the dependencies of Class A. 我需要填充Class A的所有依赖项的Testing Class。

@RunWith(MockitoJUnitRunner.class)
public class ATest {     

    @InjectMocks
    A a = new A();    

    @Mock
    private B b;
    @Mock
    private C c;

    @Mock
    private X x;
    @Mock
    private Y y;
}

You can do next. 你可以做下一个。 In this case, B will be spy object so you can mock methods results on it if needed. 在这种情况下,B将是间谍对象,因此如果需要,您可以在其上模拟方法结果。 Or you can use real B methods with mocked X and Y methods. 或者你可以使用真实的B方法和模拟的X和Y方法。

    @RunWith(MockitoJUnitRunner.class)
public class ATest {        

    @Mock
    private X x;
    @Mock
    private Y y;


        @Spy
        @InjectMocks
        private B b;

        @Mock
        private C c;

        @InjectMocks
        A a; 

        @Before
        public void setUp() {
           MockitoAnnotations.initMock(this);
        }

    }

If you want test your class A , you don't need to mock class X and Y . 如果你想测试你的A类,你不需要模拟类XY You should mock only class B and C and of course you have to specify what your mock objects return when they were invoked. 您应该只模拟B类和C类,当然您必须指定模拟对象在调用时返回的内容。

Here is simple example of your class with one method. 以下是使用一种方法的类的简单示例。

@Named
@Scope(value = "prototype")
public class A {
    @Inject
    private B b;
    @Inject
    private C c;

    public int someMethod(){
        int value = b.otherMethod();

        return Math.abs(value);
    }
}

@Named
@Scope(value = "prototype")
 class B {
    @Inject
    private X x;
    @Inject
    private Y y;

    public int otherMethod(){
        int value = x.something();
        int otherValuey = y.something();

        return value + otherValuey;
    }
}

And your test might look like this. 你的测试可能看起来像这样。

@RunWith(MockitoJUnitRunner.class)
public class ATest {
    @InjectMocks
    private A a;

    //mock only B and C
    @Mock
    private B b;
    @Mock
    private C c;

    public void shouldTestSomething(){
        //given
        Mockito.when(b.otherMethod()).thenReturn(-1); //you specified what happen when method will invoked

        //when
        int value = a.someMethod();

        //then
        Assert.assertEquals(1, value);
    }
}

You can use Springs test runner with a test application context xml. 您可以将Springs测试运行器与测试应用程序上下文xml一起使用。

In this test application context you can create any mock simply with using springs factory method attribute on the Mockito class: 在此测试应用程序上下文中,您可以使用Mockito类上的spring factory方法属性创建任何模拟:

<bean id="mockBean" class="org.mockito.Mockito" factory-method="mock"> 
    <constructor-arg value="com.package.ClassToBeMocked" /> 
</bean>

Then you can inject this mockBean into your other bean. 然后你可以将这个mockBean注入你的另一个bean。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM