У меня есть метод void, а именно invokeEllaAsync, который я хотел бы протестировать в экосистеме Spring и который приведен ниже. Метод также вызывает другой метод void изнутри как вложенный вызов.
Информация о насмешках приведена ниже,
@InjectMocks
private EllaService ellaService;
private IrisBo validIrisBo;
@Mock
private EllaRequestDto ellaRequestDtoMock;
@Mock
private EntityServiceConnectable<EllaResponseDto> connectorMock;
@Mock
private EllaResponseDto ellaResponseMock;
@Async
public void invokeEllaAsync( final IrisBo irisBo ) throws EllaGatewayUnsuccessfulResponseException {
try {
callEllaService( irisBo );
}
/**
* Asynchronously call Ella. Determine if traffic is applicable for Ella and if yes forward to Ella.
*
* @param irisEo
* @return List<ResultBo>
* @throws EllaGatewayUnsuccessfulResponseException
*/
catch( EllaGatewayUnsuccessfulResponseException ex ) {
throw new EllaGatewayUnsuccessfulResponseException( ex.getMessage(), ex.getCause() );
}
}
private void callEllaService( final IrisBo irisBo ) throws EllaGatewayUnsuccessfulResponseException {
HttpHeaders elladHeaders = createRequestHeaders( irisBo );
ServiceResponse<EllaResponseDto> response = connector.call( EllaDtoConverter.convertToRequest( irisBo ), elladHeaders );
if( !response.isSuccess() ) {
throw new EllaGatewayUnsuccessfulResponseException( response.getErrorMessage(), response.getException().getCause() );
}
}
Я пытаюсь проверить метод invokeEllaAsync
следующим образом,
@Test
public void testInvokeEllaShowsSuccess() {
ServiceResponse<EllaResponseDto> validServiceResponseMock = mock( ServiceResponse.class );
when( connectorMock.call( any(), (HttpHeaders) any() ) ).thenReturn( validServiceResponseMock );
when( validServiceResponseMock.isSuccess() ).thenReturn( true );
ellaService.invokeEllaAsync( validIrisBo );
verify( ellaService, times( 1 ) ).invokeEllaAsync( validIrisBo );
}
Я получаю ошибку, указанную ниже,
org.mockito.exceptions.misusing.NotAMockException:
Argument passed to verify() is of type EllaService and is not a mock!
Make sure you place the parenthesis correctly!
See the examples of correct verifications:
verify(mock).someMethod();
verify(mock, times(10)).someMethod();
verify(mock, atLeastOnce()).someMethod();
Если я правильно понимаю, ellaService
- это тип @InjectMocks
, а не @Mock
. Следовательно, тест не выполняется.
Как правильно написать тест?