mercredi 1 août 2018

Mocking class is returning null instead of data

In my Junit test, I'm doing the following in my Junit test :

   @Before
    public void setUp() throws Exception {

        reportQueryParams = ReportQueryParams.builder()
            .id("07")
            .build();
    }

    @Test
    public void tabSerializerTest() {
        MetricsSerializer mockMonth = mock(MetricsSerializer.class);
            when(mockMonth.getCurrentMonth()).thenReturn("July");
        String tabSeparated = mockMonth.serializeMetrics(reportQueryParams);
        String expected = new StringBuilder().append("074")
            .append("\t")
            .append("July")
            .toString();
        assertEquals(expected, tabSeparated);

}

The function which I am testing:

public String serializeMetrics(final ReportQueryParams reportQueryParams) {
    stringJoiner = new StringJoiner("\t");
    addValueFromString(reportQueryParams.getId());
    addValueFromString(getCurrentMonth());
    return stringJoiner.toString();
}

public String getCurrentMonth() {
    DateFormat monthFormat = new SimpleDateFormat("MMMMM");
    return monthFormat.format(new Date());
}


private void addValueFromString(final String value) {
    stringJoiner.add(value);
}

My ReportQueryParams class:

  public class ReportQueryParams {
        private String id;
    }

I am getting "null" in the actual data returned and hence the test is failing. How can I fix this?

Aucun commentaire:

Enregistrer un commentaire