vendredi 18 septembre 2015

Mockito with Spring

For one of my use case, I have to mock an autowired dependency only a single test case, while I want other tests to use the original one.

<Code>
public class A {

    @Autowired
    private B b;
}

Class TestA {

    @Autowired
    private A a;

    void test1() {
    //using B without mock.
    }

    void test2() {
    // mock B b in A here
    }
}
</Code>

I want to mock the private class variable 'b' here in some particular tests. I know if I have to mock B in entire class I can use @Mock, @InjectMocks and MockitoAnnotations.initMocks(), but that will mock 'b' for the other test cases as well where I want original behavior.



from Newest questions tagged java - Stack Overflow http://ift.tt/1P6S4VH
via IFTTT

Aucun commentaire:

Enregistrer un commentaire