mercredi 10 juin 2015

Powermock-Mockito: Mock private methods of InjectMock field

I'm writing a junit 4 test case. I'm using @InjectMocks to inject mocks into a field (say myField) which is using @Autowired. I need to mock private method of myField to throw some Exception. But when I'm writing the following code

PowerMockito.doThrow(new Exception()).when(myField, "privateMethod");

I'm getting exception from PowerMockito:

org.mockito.exceptions.misusing.UnfinishedStubbingException: 
[junit] Unfinished stubbing detected here:
[junit] -> at org.powermock.api.mockito.internal.PowerMockitoCore.doAnswer(PowerMockitoCore.java:36)
[junit] 
[junit] E.g. thenReturn() may be missing.
[junit] Examples of correct stubbing:
[junit]     when(mock.isOk()).thenReturn(true);
[junit]     when(mock.isOk()).thenThrow(exception);
[junit]     doThrow(exception).when(mock).someVoidMethod();
[junit] Hints:
[junit]  1. missing thenReturn()
[junit]  2. you are trying to stub a final method, you naughty developer!
[junit]  3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed

Aucun commentaire:

Enregistrer un commentaire