2021-08-02

This commit is contained in:
2021-08-02 17:02:06 +09:00
parent 7d3ad1cce0
commit d45139dd58
79 changed files with 1685 additions and 152 deletions

View 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);
}
}

View 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();
}
}

View 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.";
}
}

View File

@@ -0,0 +1,8 @@
/*
* Spring-boot Examples
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;

View File

@@ -0,0 +1,2 @@
<h1>HOME</h1>
<p>Hello, {{name}}!</p>

View File

@@ -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();
}
}

View File

@@ -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());
}
}

View File

@@ -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")));
}
}

View File

@@ -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")));
}
}