简体   繁体   中英

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. The method also calls another void method from inside as a nested call.

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,

    @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 . 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). EllaService is not a moc, but instance of real class from your code. @ InjectMock annotation just set private fields of EllaService with mocks.

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.

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() );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM