Spring Boot Rest Test Example Code

Posted by dogukanhan on September 04, 2020 · 5 mins read

Simple example code for Spring Boot Rest, testing GET, POST and DELETE http methods.

Students stored in memory without any persistance so there is no repository or service in the controller.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@RestController
@RequestMapping("/student")
public class StudentController {

    private static final Map<Integer, Student> students = new HashMap<>();

    @GetMapping
    public List<Student> students() {
        return new ArrayList<>(students.values());
    }

    @PostMapping
    public Student create(@RequestBody Student student) {
        students.put(student.getId(), student);
        return student;
    }

    @DeleteMapping
    public ResponseEntity del(@RequestBody Student student) {
        students.remove(student.getId());
        return ResponseEntity.noContent().build();
    }

}

Entity class without any annotation.

1
2
3
4
5
public class Student {
    private int id;

    private String name;
}

Test class.

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
import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.JSONArray;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;

import java.nio.charset.StandardCharsets;
import java.util.Collections;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(StudentController.class)
public class StudentWebMockTest {

    private static final String STUDENT_PATH = "/student";

    @Autowired
    private MockMvc mockMvc;

    @Autowired
    private ObjectMapper mapper;

    @Test
    void studentCreateTest() throws Exception {

      // GET 
        //Test empty student list
        this.mockMvc.perform(get(STUDENT_PATH))
                .andExpect(status().isOk())
                .andExpect(content().json(new JSONArray().toString()));

	      // POST
        // Create Student
        Student student = new Student(1, "dogukan");
        this.mockMvc.perform(
                post(STUDENT_PATH)
                        .content(mapper.writeValueAsString(student))
                        .contentType(MediaType.APPLICATION_JSON)
                        .characterEncoding(StandardCharsets.UTF_8.name()))
                .andExpect(status().isOk())
                .andExpect(content().json(
                        mapper.writeValueAsString(student)
                ));
         // GET
        // List All Students and see the created student.
        this.mockMvc.perform(get(STUDENT_PATH))
                .andExpect(status().isOk())
                .andExpect(content().json(mapper.writeValueAsString(Collections.singletonList(student))));

        // Delete Student
        this.mockMvc.perform(delete(STUDENT_PATH)
                .content(mapper.writeValueAsString(student))
                .contentType(MediaType.APPLICATION_JSON)
                .characterEncoding(StandardCharsets.UTF_8.name()))
                .andExpect(status().isNoContent());
    
		    //GET 
        //List All Students after deleting test.
        this.mockMvc.perform(get(STUDENT_PATH))
                .andExpect(status().isOk())
                .andExpect(content().json(new JSONArray().toString()));
    }

}