lundi 31 août 2015

Make a class to be a Mockito mock without calling mock

I have a Java application based on Spring that uses services. For some reasons, I need to autowire a service that is implemented for for the sake of unit testing. I would like this autowired class to be a Mockito mock, such that I can pass it to all the Mockito methods (when(), etc.). Should I extend or implement some Mockito class?

E.g.

@Profile("test")
@Primary
@Service
public class MockAService implements AService {
    public void callMethod(){}
}

Then in the test I have

{
    System.setProperty("spring.profiles.active", "test");
}

@Autowired AService aservice;

and in the test method I want to do:

@Test
public void test(){
  doNothing().when(aService).callMethod();
}

Aucun commentaire:

Enregistrer un commentaire