Deepak Negi
Deepak Negi

Reputation: 119

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 . but X and Y are null even though i have mocked them. is there any way in Mockito to mock the Objects of member of member inside the 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.

@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;
}

Upvotes: 3

Views: 3338

Answers (3)

Mateusz Korwel
Mateusz Korwel

Reputation: 1148

If you want test your class A, you don't need to mock class X and Y. You should mock only class B and C and of course you have to specify what your mock objects return when they were invoked.

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

Upvotes: 2

Gábor Lipták
Gábor Lipták

Reputation: 9776

You can use Springs test runner with a test application context xml.

In this test application context you can create any mock simply with using springs factory method attribute on the Mockito class:

<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.

Upvotes: 0

adushenin
adushenin

Reputation: 269

You can do next. In this case, B will be spy object so you can mock methods results on it if needed. Or you can use real B methods with mocked X and Y methods.

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

    }

Upvotes: 2

Related Questions