-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathMemberControllerTests.java
More file actions
89 lines (73 loc) · 3.29 KB
/
Copy pathMemberControllerTests.java
File metadata and controls
89 lines (73 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package hello;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class MemberControllerTests {
@Autowired
private MockMvc mockMvc;
@Test
public void getMemberTest() throws Exception {
this.mockMvc.perform(get("/member")).andDo(print()).andExpect(status().isOk())
.andExpect(jsonPath("$.birthDate").value("2001-04-20"));
}
@Test
public void testJava8DateTimeJacksonDefault() throws JsonProcessingException {
Member member = new Member();
member.setId(1L);
member.setFirstName("Tom");
member.setLastName("Sawyer");
member.setBirthDate(LocalDate.of(2001, Month.APRIL, 20));
member.setRegisteredDateTime(LocalDateTime.of(
2017, Month.JUNE, 29, 20, 40, 59));
ObjectMapper objectMapper = new ObjectMapper();
System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(member));
}
@Test
public void testJava8DateTimeJacksonJsr310() throws JsonProcessingException {
Member member = new Member();
member.setId(1L);
member.setFirstName("Tom");
member.setLastName("Sawyer");
member.setBirthDate(LocalDate.of(2001, Month.APRIL, 20));
member.setRegisteredDateTime(LocalDateTime.of(
2017, Month.JUNE, 29, 20, 40, 59));
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
System.out.println(objectMapper.writerWithDefaultPrettyPrinter()
.writeValueAsString(member));
}
}