2

I am facing issue related to OffsetDateTimeStamp while writing test case for my webservice. When I test from browser, it's gives right response but while writing test case it's not showing offset and because of that it's failing.

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class TestControllerTest {

    @LocalServerPort
    private int port;
    private WebClient client;
    
    @Test
    public void test() {
        Person person = new Person();
        person.setId(1);
        person.setBirthDate(OffsetDateTime.now());
        person.setMobile(9090909090L);
        person.setName("Tempo");
        client = WebClient.create("http://localhost:"+port);
        Person response = client.post().uri("/test1")
                .contentType(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON)
                .body(BodyInserters.fromValue(person))
                .retrieve()
                .bodyToMono(Person.class)
                .block();
        
        Assertions.assertEquals(person.getBirthDate(), response.getBirthDate());
    }
}

Controller Code

@RestController
public class TestController {

    @PostMapping("/test1")
    public Mono<Person> test1(@RequestBody Person person) {
        System.out.println(person.getBirthDate());
        return Mono.just(person);
    }
}

Mail Application code

@SpringBootApplication
public class TestAppApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestAppApplication.class, args);
    }

    @Bean
    public Module javaTimeModule() {
        return new JavaTimeModule();
    }

    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jacksonObjectMapperCustomization() {
        return jacksonObjectMapperBuilder -> jacksonObjectMapperBuilder.timeZone(TimeZone.getDefault());
    }
}

Person.java

public class Person {
    
    private int id;
    private String name;
    private long mobile;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
    private OffsetDateTime birthDate;
    public Person() {}
}

application.properties

spring.jackson.serialization.write-dates-as-timestamps=false
spring.jackson.deserialization.adjust-dates-to-context-time-zone=false
spring.jackson.serialization.write-dates-with-zone-id=true

output of test case

org.opentest4j.AssertionFailedError: 
Expected :2021-01-04T17:43:51.817+05:30
Actual   :2021-01-04T12:13:51.817Z
<Click to see difference>
MRX
  • 1,611
  • 8
  • 33
  • 55

1 Answers1

0

How is your property birthDate defined in your Person entity class? You need to define the format there. You can do it like this:

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ")
private OffsetDateTime birthDate;
...
public OffsetDateTime getBirthDate() {
    return birthDate;
}

See more detailed info in the answers to this question: Spring Data JPA - ZonedDateTime format for json serialization

Michael Gantman
  • 7,315
  • 2
  • 19
  • 36
  • I have added entity class. Mine is little different but both are not working. – MRX Jan 04 '21 at 12:50
  • Try to change your assertion: Instead of Assertions.assertEquals(person.getBirthDate(), response.getBirthDate()); try Assertions.assertTrue((person.getBirthDate().equals(response.getBirthDate)) – Michael Gantman Jan 04 '21 at 12:57