Вопрос

I have a form handler in ATG having following handle method -

    public boolean handleFindCards(DynamoHttpServletRequest pRequest, DynamoHttpServletResponse pResponse)
        throws IOException, ServletException {

    Card card = cardService.getCard();

    if (card == null) {
         if (isLoggingError()) {
             logError("Card Service is null");
         }
    } else {
        // Other code
    }

    return checkFormRedirect(getSuccessURL(), getSuccessURL(), pRequest, pResponse);
}

Form handler is using logging from one of it's super class GenericService. I want to write a test to check if cardService is null. In above code when card service is null, it's writing an error message using logError. Can we use ArgumentCaptor here to verify the message. OR is there any other way to test this scenario?

Here, how mocking and spying will work together? I should stub the call for getting card object as -

when(cardServiceMock.getCard()).thenReturn(cardMock);

This is my test method -

    @Test
    public void testHandleCardServiceIfCardIsNull() throws IOException, ServletException {
        CardService cardServiceMock = mock(CardService.class);
        when(cardServiceMock.getCard()).thenReturn(null);

        CardOperationsFormHandler testObjSpy = spy(new CardOperationsFormHandler());
        testObjSpy.handleFindCards(requestMock, responseMock);
        ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class);
        Mockito.verify(testObjSpy).logError(argumentCaptor.capture());
        Assert.assertEquals(argumentCaptor.getValue(), "Card Service is null");
    }

Thanks.

Это было полезно?

Решение

As David Wallace has said, you'll need to use Mockito.spy(T object).

So with a simple method:

public void runArgumentCaptor() {
    logError("This is an error");
}

The test can look like this:

    @Spy @InjectMocks private SomeClassName testObj;

    @Test(groups = { "unit" })
    public void runArgumentCaptorTest() throws Exception {

        testObj.runArgumentCaptor();

        ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class);
        Mockito.verify(testObj).logError(argumentCaptor.capture());
        Assert.assertEquals(argumentCaptor.getValue(), "This is an error");
    }

[EDIT] Added the declaration of testObj. Note the additional @Spy. Also updated the test method.

Другие советы

It's working now. As radimpe said I have replaced mock with spy. So, here I am creating cardServiceSpy instead of cardServiceMock and then using stub. Also, need to use doReturn for stubbing. Here is the method -

    @Test
    public void testHandleCardServiceIfCardIsNull() throws IOException, ServletException {
        CardService cardServiceSpy = spy(new CardService());
        doReturn(null).when(cardServiceSpy).getCard();

        CardOperationsFormHandler testObjSpy = spy(new CardOperationsFormHandler());
        testObjSpy.handleFindCards(requestMock, responseMock);
        ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class);
        Mockito.verify(testObjSpy).logError(argumentCaptor.capture());
        Assert.assertEquals(argumentCaptor.getValue(), "Card Service is null");
    }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top