Skip to content

Commit 7d5a755

Browse files
authored
Merge pull request eugenp#2958 from yasin3061/master
BAEL-1217: Guide to Spring Type Conversions
2 parents 582147e + b32cd7a commit 7d5a755

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.baeldung.converter.controller;
2+
3+
import com.baeldung.toggle.Employee;
4+
import org.springframework.http.MediaType;
5+
import org.springframework.http.ResponseEntity;
6+
import org.springframework.web.bind.annotation.*;
7+
8+
@RestController
9+
@RequestMapping("/string-to-employee")
10+
public class StringToEmployeeConverterController {
11+
12+
@GetMapping
13+
public ResponseEntity<Object> getStringToEmployee(@RequestParam("employee") Employee employee) {
14+
return ResponseEntity.ok(employee);
15+
}
16+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.baeldung.converter.controller;
2+
3+
import org.baeldung.Application;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
8+
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.test.context.junit4.SpringRunner;
10+
import org.springframework.test.web.servlet.MockMvc;
11+
12+
import static org.hamcrest.CoreMatchers.is;
13+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
14+
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
15+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
16+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
17+
18+
@RunWith(SpringRunner.class)
19+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class)
20+
@AutoConfigureMockMvc
21+
public class StringToEmployeeConverterControllerTest {
22+
23+
@Autowired
24+
private MockMvc mockMvc;
25+
26+
@Test
27+
public void getStringToEmployeeTest() throws Exception {
28+
mockMvc.perform(get("/string-to-employee?employee=1,2000"))
29+
.andDo(print())
30+
.andExpect(jsonPath("$.id", is(1)))
31+
.andExpect(jsonPath("$.salary", is(2000.0)))
32+
.andExpect(status().isOk());
33+
}
34+
}

0 commit comments

Comments
 (0)