Posts 스프링 MockMvc를 이용한 get, post, patch, delete 테스트
Post
Cancel

스프링 MockMvc를 이용한 get, post, patch, delete 테스트

MockMvc란?

MockMvc는 어플리케이션을 서버에 배포하지 않고도
스프링 MVC의 테스트를 진행할 수 있게 도와주는 클래스이다.

POST 테스트

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
@Autowired
private MockMvc mockMvc;

@Autowired
private ObjectMapper objectMapper;

@DisplayName("회원가입 테스트")
@Test
public void signupTest() throws Exception {

    Map<String, String> input = new HashMap<>();
    // body에 json 형식으로 회원의 데이터를 넣기 위해서 Map을 이용한다.
    input.put("name", "test2");
    input.put("email", "test2@google.com");
    input.put("password", "test2_password");

    mockMvc.perform(post("/signup")
            .contentType(MediaType.APPLICATION_JSON) 
            //json 형식으로 데이터를 보낸다고 명시
            .content(objectMapper.writeValueAsString(input)))
            //Map으로 만든 input을 json형식의 String으로 만들기 위해 objectMapper를 사용
            .andExpect(status().isOk())
            //Http 200을 기대
            .andDo(print());
            //화면에 결과를 출력
}  


GET 테스트

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
@Autowired
private MockMvc mockMvc;

@Autowired
private UserRepository userRepository;

@Autowired
private ObjectMapper objectMapper;

@DisplayName("회원정보 조회")
@Test
public void findUserByEmailTest() throws Exception {

    User user = User.builder()
            .name("hello")
            .email("hello@google.com")
            .password("test_password")
            .role("ROLE_USER")
            .build();

    userRepository.save(user);

	mockMvc.perform(get("/user/hello@google.com"))
			.andExpect(status().isOk())
			.andDo(print());
}


PATCH 테스트

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
@Autowired
private MockMvc mockMvc;

@Autowired
private UserRepository userRepository;

@Autowired
private ObjectMapper objectMapper;

@DisplayName("회원정보 수정")
@Test
public void fixUserInfo() throws Exception {
    User user = User.builder()
            .name("test")
            .email("test3@google.com")
            .password("test_password")
            .role("ROLE_USER")
            .build();

    userRepository.save(user);

    Map<String, String> input = new HashMap<>();
    input.put("name", "test100");
    input.put("password", "test100_password");

    mockMvc.perform(patch("/user/test3@google.com")
            .contentType(MediaType.APPLICATION_JSON)
            .content(objectMapper.writeValueAsString(input)))
            .andExpect(status().isOk())
            .andDo(print());
}


DELETE 테스트

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
@Autowired
private MockMvc mockMvc;

@Autowired
private UserRepository userRepository;

@DisplayName("회원정보 삭제")
@Test
public void deleteUserTest() throws Exception {

	User user = User.builder()
			.name("hello2")
			.email("hello4@google.com")
			.password("test_password")
			.role("ROLE_USER")
			.build();

	userRepository.save(user);

	mockMvc.perform(delete("/user/hello4@google.com"))
			.andExpect(status().isOk())
			.andDo(print());

	Assertions.assertThat(userRepository.findByEmail("hello4@google.com").isEmpty());
}


전체 코드

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
90
@SpringBootTest
@AutoConfigureMockMvc
class UserControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private UserRepository userRepository;

    @DisplayName("회원가입 테스트")
    @Test
    public void signupTest() throws Exception {
        Map<String, String> input = new HashMap<>();
        input.put("name", "test2");
        input.put("email", "test2@google.com");
        input.put("password", "test2_password");

        mockMvc.perform(post("/signup")
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(input)))
                .andExpect(status().isOk())
                .andDo(print());
    }

    @DisplayName("회원정보 조회")
    @Test
    public void findUserByEmailTest() throws Exception {

        User user = User.builder()
                .name("hello")
                .email("hello@google.com")
                .password("test_password")
                .role("ROLE_USER")
                .build();

        userRepository.save(user);

        mockMvc.perform(get("/user/hello@google.com"))
                .andExpect(status().isOk())
                .andDo(print());
    }


    @DisplayName("회원정보 수정")
    @Test
    public void fixUserInfo() throws Exception {
        User user = User.builder()
                .name("test")
                .email("test3@google.com")
                .password("test_password")
                .role("ROLE_USER")
                .build();

        userRepository.save(user);

        Map<String, String> input = new HashMap<>();
        input.put("name", "test100");
        input.put("password", "test100_password");

        mockMvc.perform(patch("/user/test3@google.com")
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(input)))
                .andExpect(status().isOk())
                .andDo(print());
    }

    @DisplayName("회원정보 삭제")
    @Test
    public void deleteUserTest() throws Exception {

        User user = User.builder()
                .name("hello2")
                .email("hello4@google.com")
                .password("test_password")
                .role("ROLE_USER")
                .build();

        userRepository.save(user);

        mockMvc.perform(delete("/user/hello4@google.com"))
                .andExpect(status().isOk())
                .andDo(print());

        Assertions.assertThat(userRepository.findByEmail("hello4@google.com").isEmpty());
    }
}

스프링 테스트 오류 Test events were not received

단절점 알고리즘

Comments powered by Disqus.