-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathHelloControllerTest.java
52 lines (41 loc) · 1.85 KB
/
HelloControllerTest.java
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
package com.ceos21.spring_boot.controller;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
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.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import static org.assertj.core.api.Assertions.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@AutoConfigureMockMvc
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
public class HelloControllerTest {
@Autowired
private MockMvc mvc;
@Test
@DisplayName("Hello Controller success test")
public void getHello_success_test() throws Exception {
// given
String expectedResult = "Greeting from Spring boot";
// when
ResultActions result = mvc.perform(get("/").contentType(MediaType.APPLICATION_JSON));
String responseBody = result.andReturn().getResponse().getContentAsString();
System.out.println("성공 테스트 : " + responseBody);
// then
assertThat(responseBody).isEqualTo(expectedResult);
}
@Test
@DisplayName("Hello Controller fail Test : URL Not FOUND")
public void getHello_fail_test() throws Exception {
// given
String invalidUrl = "/invalid";
//when
ResultActions result = mvc.perform(MockMvcRequestBuilders.get(invalidUrl).accept(MediaType.APPLICATION_JSON));
// then
result.andExpect(status().isNotFound());
}
}