|
| 1 | +package dev.subhransu.fasterxmljacksonexamples.JsonAnyGetterSetter; |
| 2 | + |
| 3 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
| 4 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
| 5 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 6 | + |
| 7 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 8 | +import java.util.Map; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 12 | +import org.springframework.boot.test.context.SpringBootTest; |
| 13 | +import org.springframework.http.MediaType; |
| 14 | +import org.springframework.test.web.servlet.MockMvc; |
| 15 | + |
| 16 | +@SpringBootTest |
| 17 | +@AutoConfigureMockMvc |
| 18 | +class JsonAnyGetterSetterExampleTest { |
| 19 | + |
| 20 | + @Autowired private MockMvc mockMvc; |
| 21 | + @Autowired private ObjectMapper objectMapper; |
| 22 | + |
| 23 | + @Test |
| 24 | + void testAnyGetterSetter() throws Exception { |
| 25 | + JsonAnyGetterSetterExample input = |
| 26 | + new JsonAnyGetterSetterExample( |
| 27 | + "John Doe", |
| 28 | + objectMapper.readValue( |
| 29 | + """ |
| 30 | + { |
| 31 | + "age": 30, |
| 32 | + "city": "New York", |
| 33 | + "address": { |
| 34 | + "street": "123 Main St", |
| 35 | + "zip": "10001" |
| 36 | + } |
| 37 | + } |
| 38 | + """, |
| 39 | + objectMapper |
| 40 | + .getTypeFactory() |
| 41 | + .constructMapType(Map.class, String.class, Object.class))); |
| 42 | + |
| 43 | + mockMvc |
| 44 | + .perform( |
| 45 | + post("/json-any-getter-setter") |
| 46 | + .contentType(MediaType.APPLICATION_JSON) |
| 47 | + .content(objectMapper.writeValueAsString(input))) |
| 48 | + .andExpect(status().isOk()) |
| 49 | + .andExpect(jsonPath("$.name").value("John Doe")) |
| 50 | + .andExpect(jsonPath("$.age").value(30)) |
| 51 | + .andExpect(jsonPath("$.city").value("New York")) |
| 52 | + .andExpect(jsonPath("$.address.street").value("123 Main St")) |
| 53 | + .andExpect(jsonPath("$.address.zip").value("10001")); |
| 54 | + } |
| 55 | +} |
0 commit comments