I'm trying to verify that my reactive rest controller transfers the correct data. This data contains a ZonedDateTime
field I need to retain. However, when querying the rest controller with a WebTestClient
, my verification fails because the received time is suddenly in UTC.
@Data
public class SimpleData {
ZonedDateTime zdt;
}
@RestController
class SimpleDataController {
@Autowired SimpleDataService service;
@GetMapping("/simple")
List<SimpleData> getData() {
return service.getTimes();
}
}
@Service
class SimpleDataService {
public static final SimpleData DATA = new SimpleData();
static {
DATA.setZdt(ZonedDateTime.now(ZoneId.of("Europe/Berlin")));
}
public List<SimpleData> getTimes() {
return List.of(DATA);
}
}
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@ActiveProfiles("test")
class ApplicationTests {
@Test
void simpleDataTest() {
List<SimpleData> fromRest = WebTestClient.bindToServer().baseUrl("http://localhost:8080").build()
.get().uri("/simple").exchange()
.expectBodyList(SimpleData.class)
.returnResult().getResponseBody();
assertThat(fromRest).containsAll(Collections.singletonList(SimpleDataService.DATA));
}
}
This fails with
Expecting ArrayList: <[SimpleData(zdt=2020-08-05T09:30:40.291415300Z[UTC])]> to contain: <[SimpleData(zdt=2020-08-05T11:30:40.291415300+02:00[Europe/Berlin])]> but could not find the following element(s): <[SimpleData(zdt=2020-08-05T11:30:40.291415300+02:00[Europe/Berlin])]>
The time itself is correct - the time zone difference is substracted from the hour field - but it fails the equals obviously. Funnily enough, if you query the url with a client, the JSON contains the correct data:
[{"zdt":"2020-08-05T11:44:10.4740259+02:00"}]
It seems to be the TestWebClient
converting the time.
Is this intended? Can I change this behaviour somehow?