lundi 28 décembre 2015

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

Aucun commentaire:

Enregistrer un commentaire