-1

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?

Tee Jay
  • 103
  • 1
  • 1
  • 14

2 Answers2

0

Don't mock the object you test.What you have written is "create a mock object that returns July for current month". But this mock object doesn't have real behaviour and the other methods return null.

When you test a class you mock the objects required by the class (in order to insulate behaviour) and not the actual class. Here you can create a new MetricsSerializer (by using new :) and call it's method serializeMethod and compare against the current date (instead of July).

The way you have written the class might not be the best testable way though ;)

Veselin Davidov
  • 7,031
  • 1
  • 15
  • 23
  • What i initially tried was : MetricsSerializer metricsSerializer = new MetricsSerializer(); and then I called . String tabSeparated = metricsSeriazlier.serializeMetrics(reportQueryParams, remoteUser); This returns the data, but I want to get something to compare against the current month. How can I compare against July? – Tee Jay Aug 01 '18 at 21:34
  • You cannot compare against July because your method is made in a way that it uses the current date. If you compare against July it will fail and the correct behavior of the method is not to return July. Just create a new Date() in the test and compare the months – Veselin Davidov Aug 02 '18 at 07:16
0

Your problem is that you are mocking the class, then testing the mock object, rather than testing a "real" object. I can think of two possible solutions.

  1. Use a Mockito Spy instead of a mock. This is like a mock, but it's a real object, and the methods all have their normal behaviour, instead of "no behaviour" by default. You can stub the getCurrentMonth method of your spy, to make it return what you want.

  2. Since the real cause of your problem is the call to new Date(), you could use a time helper, instead of calling new Date() directly in your getCurrentMonth() method. I have described this technique in detail in my answer to this question

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110