Mockito query

Soldato
Joined
1 Mar 2003
Posts
5,508
Location
Cotham, Bristol
I'm in the process of writing unit tests for my Spring MVC application, one of the GET requests I am testing has the following work flow

1. Send get request with date string as part of the URI
2. Controller receives request and validates date string to check it's in the expected format.
3. Controller calls service method passing the validated date string
4. Service converts date string in to date object and passes that on to the repository method.

Now I'm unit testing the service layer currently and in my test I have something like

Code:
Programme[] programmes = new Programme[]{prog1};
when(repositoryMock.findProgForPeriod(TestUtils.TODAY)).thenReturn(Arrays.asList(programmes));

List<Programme> progList = service.findProgForPeriod("20140828");

assertEquals("Prog list is of wrong size: ", 1, progList.size());

Now the problem is because TestUtils.TODAY and the Date object the service creates are different objects (albeit having the same date/time) then the test fails as the size of progList is 0.

I could move the conversion from String to Date up to the Controller, but I'd rather keep business logic like this in the Service layer. Any suggestions, my mind has gone blank.
 
Last edited:
Caporegime
Joined
18 Oct 2002
Posts
29,491
Location
Back in East London
You just need to ensure that whatever is passed to the mock is what you expect it to be. If you're passing in a string date, then surely you're expecting a Date object that represents that date as well. So don't use "TODAY" for anything, unless you want your test to break tomorrow.

Either format the TestUtils.TODAY with a ToString() or whatever converter you need, or in the mock's expectation generate a Date object using the same input string as you are sending.

I also encourage you to use ISO format date strings if you are going to be sending them as strings and not Date. Infact I'm pretty sure that the Java serialisers use these formats anyway.
 
Soldato
OP
Joined
1 Mar 2003
Posts
5,508
Location
Cotham, Bristol
You just need to ensure that whatever is passed to the mock is what you expect it to be. If you're passing in a string date, then surely you're expecting a Date object that represents that date as well. So don't use "TODAY" for anything, unless you want your test to break tomorrow.

Either format the TestUtils.TODAY with a ToString() or whatever converter you need, or in the mock's expectation generate a Date object using the same input string as you are sending.

I also encourage you to use ISO format date strings if you are going to be sending them as strings and not Date. Infact I'm pretty sure that the Java serialisers use these formats anyway.

Sorry my over simplification of the code has obviously confused things :p, let me try again

The test

Code:
@Test
public void testFindProgForPeriodWhenProgIsFound() throws Exception {
  Programme[] programmes = new Programme[]{prog1};
  when(repositoryMock.findProgForPeriod(TestUtil.TODAY)).thenReturn(Arrays.asList(programmes));

  List<Programme> progList = service.findProgForPeriod(new DateTime(TestUtil.TODAY).toString(TestUtil.DATE_TIME_FORMAT));

  assertEquals("Prog list is of wrong size: ", 1, progList.size());
}

The service method

Code:
  public List<Programme> findProgForPeriod(String date) {    
    Date from = Utils.DATE_TIME_FORMATTER.parseDateTime(date).toDate();
    return programmeRepository.findProgForPeriod(from);
  }

So as you can see I do format TODAY as a string using my formatter, so the test won't break tomorrow.
 
Last edited:
Caporegime
Joined
18 Oct 2002
Posts
29,491
Location
Back in East London
Right, so have you tested that your formatting produces equal values?

e: specifically does Utils.DATE_TIME_FORMATTER.parseDateTime() produce what you expect? Is it equal to new DateTime(x) (and should there be a .ToDate() here?)?
 
Last edited:
Soldato
OP
Joined
1 Mar 2003
Posts
5,508
Location
Cotham, Bristol
Right, so have you tested that your formatting produces equal values?

Yes, as I said I don't think that's the issue. I think the when is being literal in what it's expecting i.e. a date object with reference x which created in the TestUtil class. But the repository method is actually being called with a date object with reference y because that's created in the service method.

Edit: It does yes, I should also have mentioned I'm using JodaTime, so parseDateTime creates a joda DateTime object which I then convert to a java.util.Date
 
Back
Top Bottom