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
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.
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: