2021-08-02
This commit is contained in:
20
testing/src/main/java/kr/pe/elex/examples/Application.java
Normal file
20
testing/src/main/java/kr/pe/elex/examples/Application.java
Normal file
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Spring-boot Examples
|
||||
*
|
||||
* Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
* https://www.elex-project.com/
|
||||
*/
|
||||
|
||||
package kr.pe.elex.examples;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
41
testing/src/main/java/kr/pe/elex/examples/MyController.java
Normal file
41
testing/src/main/java/kr/pe/elex/examples/MyController.java
Normal file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Spring-boot Examples
|
||||
*
|
||||
* Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
* https://www.elex-project.com/
|
||||
*/
|
||||
|
||||
package kr.pe.elex.examples;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
@Controller
|
||||
public class MyController {
|
||||
|
||||
@Autowired
|
||||
private MyService service;
|
||||
|
||||
@GetMapping({"/"})
|
||||
public String index(Model model) {
|
||||
model.addAttribute("name", "World");
|
||||
return "main";
|
||||
}
|
||||
|
||||
@GetMapping({"/{name}"})
|
||||
public String index(@PathVariable @ModelAttribute String name) {
|
||||
if (null == name) name = "World";
|
||||
return "main";
|
||||
}
|
||||
|
||||
@GetMapping("/greetings")
|
||||
@ResponseBody
|
||||
public String greetings() {
|
||||
return service.sayHello();
|
||||
}
|
||||
}
|
||||
17
testing/src/main/java/kr/pe/elex/examples/MyService.java
Normal file
17
testing/src/main/java/kr/pe/elex/examples/MyService.java
Normal file
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* Spring-boot Examples
|
||||
*
|
||||
* Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
* https://www.elex-project.com/
|
||||
*/
|
||||
|
||||
package kr.pe.elex.examples;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class MyService {
|
||||
public String sayHello() {
|
||||
return "Hello, World.";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
/*
|
||||
* Spring-boot Examples
|
||||
*
|
||||
* Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
* https://www.elex-project.com/
|
||||
*/
|
||||
|
||||
package kr.pe.elex.examples;
|
||||
2
testing/src/main/resources/templates/main.mustache
Normal file
2
testing/src/main/resources/templates/main.mustache
Normal file
@@ -0,0 +1,2 @@
|
||||
<h1>HOME</h1>
|
||||
<p>Hello, {{name}}!</p>
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Spring-boot Examples
|
||||
*
|
||||
* Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
* https://www.elex-project.com/
|
||||
*/
|
||||
|
||||
package kr.pe.elex.examples;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||
|
||||
/**
|
||||
* 통합 테스트. 모든 빈을 로드한 다음, 테스트가 수행된다.
|
||||
*/
|
||||
@SpringBootTest(classes = Application.class)
|
||||
class ApplicationTest {
|
||||
@Autowired
|
||||
private MyController controller;
|
||||
|
||||
@Test
|
||||
void test() {
|
||||
assertThat(controller).isNotNull();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Spring-boot Examples
|
||||
*
|
||||
* Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
* https://www.elex-project.com/
|
||||
*/
|
||||
|
||||
package kr.pe.elex.examples;
|
||||
|
||||
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.test.web.servlet.MockMvc;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
/**
|
||||
* MvcMock을 사용해서 웹 요청과 응답을 테스트할 수 있다.
|
||||
*/
|
||||
@SpringBootTest//(classes = Application.class)
|
||||
@AutoConfigureMockMvc
|
||||
class ControllerTest1 {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Test
|
||||
void getTest() throws Exception {
|
||||
this.mockMvc.perform(get("/"))
|
||||
.andDo(print())
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string(containsString("Hello, World")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getTest2() throws Exception {
|
||||
this.mockMvc.perform(get("/charlie"))
|
||||
.andDo(print())
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string(containsString("Hello, charlie")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void postTest() throws Exception {
|
||||
this.mockMvc.perform(post("/"))
|
||||
.andDo(print())
|
||||
.andExpect(status().isMethodNotAllowed());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Spring-boot Examples
|
||||
*
|
||||
* Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
* https://www.elex-project.com/
|
||||
*/
|
||||
|
||||
package kr.pe.elex.examples;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
/**
|
||||
* Mockito를 사용해서 테스트용 서비스를 모킹한다.
|
||||
*/
|
||||
@WebMvcTest(MyController.class)
|
||||
class ControllerTest2 {
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@MockBean
|
||||
private MyService service;
|
||||
|
||||
@BeforeEach
|
||||
void beforeAll() {
|
||||
Mockito.when(service.sayHello())
|
||||
.thenReturn("Hello, Mock");
|
||||
}
|
||||
|
||||
@Test
|
||||
void greetingsTest() throws Exception {
|
||||
this.mockMvc.perform(get("/greetings"))
|
||||
.andDo(print())
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string(containsString("Hello, Mock")));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Spring-boot Examples
|
||||
*
|
||||
* Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
* https://www.elex-project.com/
|
||||
*/
|
||||
|
||||
package kr.pe.elex.examples;
|
||||
|
||||
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.boot.test.context.TestConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
/**
|
||||
* 테스트 설정 클래스를 만든 다음, 테스트용 빈을 등록한다.
|
||||
*/
|
||||
@WebMvcTest(MyController.class)
|
||||
class ControllerTest3 {
|
||||
@TestConfiguration
|
||||
static class TestConfig {
|
||||
@Bean
|
||||
MyService myService() {
|
||||
return new MyService() {
|
||||
@Override
|
||||
public String sayHello() {
|
||||
return "Hello, Mock";
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Test
|
||||
void greetingsTest() throws Exception {
|
||||
this.mockMvc.perform(get("/greetings"))
|
||||
.andDo(print())
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string(containsString("Hello, Mock")));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user