package com.hl.book.springboot.web;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(){
return "hello";
}
}
package com.hl.book.springboot.web;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringRunner.class) // (1)
@WebMvcTest(controllers = HelloController.class) // (2)
public class HelloControllerTest {
@Autowired // (3)
private MockMvc mvc; // (4)
@Test
public void hello_리턴() throws Exception{
String hello = "hello";
mvc.perform(get("/hello")) // (5)
.andExpect(status().isOk()) // (6)(7)
.andExpect(content().string(hello)); // (8)
}
}
@RunWith(SpringRunner.class)
@WebMvcTest
@Autowired
private MockMvc mvc
mvc.perform(get("/hello"))
.andExpect()
status().isOk()
content().string("hello")