mercredi 22 juin 2016

Mockito when/then not working

I'm trying to Mock ValidatorService in unit tests of SubscriptionInterceptor.

validator = Mockito.mock(ValidatorService.class);
Mockito.when(validator.validateSubscription(any(), any(), any())).thenReturn("");
interceptor = new SubscriptionInterceptor(validator);

Later, when the interceptor calls the validateSubscription method of the mocked validator, an instance of the actual class is invoked, rather than the mock. Why? How can I get the method call to return an empty String?

Some additional information: ValidatorService is a Spring service with an autowired constructor that takes 2 Spring Components as arguments. SubscriptionInterceptor extends Spring's ChannelInterceptorAdapter to intercept STOMP/WebSocket subscribes. It has an autowired constructor which takes ValidatorService as the only parameter. I would like unit tests to work without running the server, by mocking dependencies etc.

Aucun commentaire:

Enregistrer un commentaire