I have made a Rest API with Spring frame work, the API will take a date and find a record from database and return the record, in the database it will have a start date and end date columns, based on the given date, it should return all records that given date is in between.
I am using oracle database java 8 and spring frome work boot 2.1.2 release, the problem is when I make a junit test with Mokito to test the REST api i made, when I pass in a date, when my method receives the date it changed the date, I have found that seems when it receives the date it converted to different timezone (+1 timezone), and my local computer timeZone is set to -6 timeZone, if I covert the date to +1, the test will be success, if I just pass in the date object, it will auto convert it to +1 timezone which will be miss match.
I would like to know why is that since I testing them in the same place, how come date still get changed, and with understanding I prefer a solution of it.
so I will have a table in data base with product_category_id(long), start_date(date), end_date(date) and second table with Product_id(long), type_code(short), price(double), product_category_id and they are associated with product_category_id
here is the method in controller file
@GetMapping("/{startDate}")
public ResponseEntity<List<ProductCategoryResource>> findByDate(
@PathVariable("startDate") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) final Date startDate)
{
final List<ProductCategory> productCategoryList =
ProductCategoryService.findByDate(startDate);
final List<ProductCategoryResource> productCategoryResourceList = new ArrayList<>();
for (final ProductCategory productCategory: productCategoryList)
{
productCategoryResourceList.add(new ProductCategoryResource(productCategory));
}
return new ResponseEntity<>(ProductCategoryResourceList, HttpStatus.OK);
}
here is service
public List<ProductCategory> findByDate(final Date searchDate)
{
return ProductCategoryRepository
.findByStartDateLessThanEqualAndEndDateGreaterThanEqual(searchDate,
searchDate);
}
and repository is just an default interface extends CrudRepository, with findByStartDateLessThanEqualAndEndDateGreaterThanEqual method inside.
here is my junit test.
public void findByDate() throws Exception{
final String pattern = "yyyy-MM-dd";
final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
final Date searchDate = simpleDateFormat.parse("2018-05-22");
//Here mock all return values and add to result list
BDDMockito.given(ProductCategoryService.findByDate(searchDate)).willReturn(resultList);
mvc.perform(MockMvcRequestBuilders.get("/productCategory/2018-05-22")
.contentType(MediaType.APPLICATION_JSON)).andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$[0].startDate", Matchers
.startsWith(simpleDateFormat.format(resultList.get(0).getProductCategoryStartDate()))));
}
so this will give an error $[0].startDate is null, which it did not return the mocked result list, I debug and found that when it hits the method in controller, the date changed to Mon May 21 18:00:00 CST 2018, so the date was not matching to the date I give in BDDMockito.given. but if I change my local timezone to +1, it will be exactly the same
I expected no matter which timezone I run the build of the project, the test never fails, it should always send same date due to it actually passed by url.