[英]Junit testing a void response method
I have a void method namely invokeEllaAsync that I would like to test in the Spring eco-system and provided below. 我有一个void方法,即invokeEllaAsync,我想在Spring生态系统中进行测试并在下面提供。 The method also calls another void method from inside as a nested call.
该方法还从内部调用另一个void方法作为嵌套调用。
The mocking information is provided below, 下面提供了模拟信息,
@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() );
}
}
I try to test the invokeEllaAsync
method as follows, 我尝试如下测试
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 );
}
I get the error provided below, 我收到下面提供的错误,
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();
If I understand it correctly, the ellaService
is the type of @InjectMocks
and not the @Mock
. 如果我理解正确的
ellaService
是类型@InjectMocks
而不是@Mock
。 Hence, the test is not executed. 因此,不执行测试。
How do I write the test correctly? 如何正确编写测试?
The problem is you can verify only mocked components (Annotated with @ Mock). 问题是您只能验证模拟的组件(用@ Mock注释)。 EllaService is not a moc, but instance of real class from your code.
EllaService不是moc,而是代码中真实类的实例。 @ InjectMock annotation just set private fields of EllaService with mocks.
@ InjectMock批注仅通过模拟设置EllaService的私有字段。
Generally when test void method you need to ensure that some mocked components were called with expected parameters, so you need to verify not ellaService call, but call to mocked components that is used in invokeEllaAsync method. 通常,在测试void方法时,您需要确保使用预期的参数调用了某些模拟组件,因此您需要验证的不是ellaService调用,而是验证对invokeEllaAsync方法中使用的模拟组件的调用。
After I read the answer, I added this test in the codebase. 阅读答案后,我将此测试添加到了代码库中。
@Test
public void testInvokeEllaAsyncSuccessfullyCallsCallEllaService() {
ellaService.invokeEllaAsync( validIrisBo );
PowerMockito.verifyStatic( EllaDtoConverter.class, VerificationModeFactory.times( 1 ) );
EllaDtoConverter.convertToRequest( validIrisBo );
verify( connectorMock, times( 1 ) ).call( any(), (HttpHeaders) any() );
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.