Reputation: 37034
I want on one method use mock eventService and in another real eventService.
@Mock(name = "eventService")
private EventService eventService;
@InjectMocks
private CandidateMenuController candidateMenuController = new CandidateMenuController();
How to write analog this code inside method.
I have CandidateMenuController candidateMenuController
as class field. But at one method I want to use specific eventService realization.
P.S I have not constructor and set get method for EventService
Upvotes: 2
Views: 468
Reputation: 10622
As you do not have any setter
method to set the value of EventService
, you can use reflection to set the value for EventService
:
@Test
public void testWithRealization() {
Field field = candidateMenuController.getClass().getDeclaredField("eventService");
field.set(candidateMenuController, new EventServiceImpl());
// Test Code
}
Upvotes: 1
Reputation: 972
Remove @MockitoJunitRunner
from class and in method where you want to use mock do MockitoAnnotations.initMocks(this);
In method where you want use specific implementation of EventService
you have to create it manually by invoking constructor.
Upvotes: 1