lundi 8 octobre 2018

PowerMockito Java - Mock inner method calls for line and mutation coverage

I have written a program that works with AWS SES and SQS for reading queue and sending emails for my work. Everything works, but I'm writing the unit tests and I cannot get line coverage above 88% and mutation coverage above 75% (line coverage minimum threshold is 95% and mutation coverage minimum is 75%. I've reached the minimum mutation, but any change to ruin that. I would like to boost that by at least 15 more. Anyway, this is a two part question... I have the following method and test. The test passes fine, but the report (jacoco?) still shows the method as light pink (no line coverage) I'm not sure who to interact with this method for line coverage:

public void sendEmail() throws IOException {
    dao = new SQSDAO();
    dao.receiveAndProcessSQSMessages();
    populateLists(dao.getReceiptHandles(), dao.getIdList());
    sendEmailOrNot(getReceiptHandles().size());
}

public void sendEmailTest() throws Exception {
    classUnderTest = PowerMockito.mock(Tickets.class);
    PowerMockito.whenNew(SQSDAO.class).withNoArguments().thenReturn(daoMock);
    daoMock.setIdList(mockList());
    daoMock.setReceiptHandles(mockList());
    PowerMockito.doNothing().when(classUnderTest).deleteConsumedMessages(daoMock);
    PowerMockito.when(daoMock.receiveAndProcessSQSMessages()).thenReturn(mockList());
    PowerMockito.when(classUnderTest.populateLists(daoMock.getReceiptHandles(), daoMock.getIdList())).thenReturn(true);
    classUnderTest.sendEmail();
    verify(classUnderTest, times(1)).sendEmail();   
}

Again.. test passes, but still no line coverage registered.

Next, how do I test method calls inside other methods?

This entire method shows mutation and line coverage (all green and light green) except for the one delete method call.

public int sendEmailIfNotEmpty(int size) {
    if (size == 0) { **<-- Covered Mutation**
        LOGGER.info("recepitHandles received... The Size is: " + size + "  ===> Skipping Ticket Creation");
        return 0;  **<-- Covered Mutation**
    } else {
        String HTMLBODY = formatEmail(getIdList());
        String TEXTBODY = receiptHandles.toString();
        Date date = new Date(System.currentTimeMillis());
        ;
        try {
            AmazonSimpleEmailService client = AmazonSimpleEmailServiceClientBuilder.standard()
                    .withRegion(Regions.US_WEST_1).build();
            SendEmailRequest request = createSendEmailRequest(HTMLBODY, TEXTBODY, dateFormat.format(date));
            client.sendEmail(request);
             LOGGER.info("Deleting Consumed Messages in SQS...");
             deleteConsumedMessages(dao);  **<--NOT COVERED!!**
        } catch (Exception ex) {
            LOGGER.info("The email was not sent. Error message: " + ex.getMessage());
        }
        return 1;  **<-- Covered Mutation**
    }
}

The Test:

@Test
public void sendEmailOrNotTest() throws Exception {
    classUnderTest = new SendExceptionTickets();
    SendExceptionTickets spy = Mockito.spy(classUnderTest);
    PowerMockito.mockStatic(AmazonSimpleEmailServiceClientBuilder.class);
    AmazonSimpleEmailServiceClientBuilder yep = PowerMockito.mock(AmazonSimpleEmailServiceClientBuilder.class);
    PowerMockito.when(yep.standard()).thenReturn(yep);
    PowerMockito.when(yep.withRegion(any(Regions.class))).thenReturn(yep);
    PowerMockito.when(yep.build()).thenReturn(emailServiceMock);
    PowerMockito.when(emailServiceMock.sendEmail(request)).thenReturn(result);
    Mockito.doNothing().when(spy).deleteConsumedMessages(daoMock);
    assertEquals(1, classUnderTest.sendEmailOrNot(24));
    assertEquals(false, classUnderTest.isMessageSuccessfullySent());
    assertEquals(0, classUnderTest.sendEmailOrNot(0));

    spy.deleteConsumedMessages(daoMock);
    verify(spy, times(1)).deleteConsumedMessages(daoMock);
}

I have tried to Mockito.spy the classUnderTest and then use verify.. but it cannot kill the delete method call mutation.

Aucun commentaire:

Enregistrer un commentaire