250x250
Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- MSSQL
- Open Source
- Spring Boot
- AJAX
- jpa
- JDBC
- oracle
- error
- MySQL
- 설정
- Docker
- Eclipse
- 오픈소스
- Exception
- SpringBoot
- Core Java
- PostgreSQL
- git
- spring
- JavaScript
- myBatis
- STS
- Python
- maven
- Thymeleaf
- ubuntu
- Source
- Tomcat
- 문서
- IntelliJ
Archives
- Today
- Total
헤르메스 LIFE
[Spring Boot] JUnit Test 본문
728x90
개발환경
Spring Boot 2.2.4
JDK 1.8.0_202
REST
Postman : REST Client
목표
1. Spring Boot REST 환경
2. Log4j2 추가
3. JWT + Spring Security 를 통한 인증
4. DB 연결 ( Hibernate 제거 )을 통한 사용자 인증
5. TDD 인증 테스트
import org.junit.*;
import org.junit.runner.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.boot.test.autoconfigure.web.servlet.*;
import org.springframework.boot.test.context.*;
import org.springframework.test.context.*;
import org.springframework.test.context.junit4.*;
import org.springframework.test.web.servlet.*;
import org.springframework.test.web.servlet.setup.*;
import org.springframework.web.context.*;
import org.springframework.web.filter.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@RunWith( SpringRunner.class )
@AutoConfigureMockMvc
@ActiveProfiles( "local" )
@SpringBootTest
public class UserControllerTest {
@Autowired
private WebApplicationContext ctx;
private MockMvc mockMvc;
@Before
public void setUp() {
this.mockMvc = MockMvcBuilders.webAppContextSetup( this.ctx ).addFilter( new CharacterEncodingFilter( "UTF-8", true ) ).build();
}
@Test
public void signinPost() throws Exception {
mockMvc.perform( post( "/v1/signin" ).queryParam( "id", "1001" ).queryParam( "password", "password" ) // POST 요청
).andDo( print() ).andExpect( status().isOk() );
}
@Test
public void findAllUser() throws Exception {
mockMvc.perform( get( "/v1/users" ).queryParam( "lang", "ko" ).header("X-AUTH-TOKEN", "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMDAxIiwicm9sZXMiOltdLCJpYXQiOjE2MTA4MTA2NDEsImV4cCI6MTYxMDg5NzA0MX0.5rHP_aEyJk0wpozI1Azy1LAJZzIx0ZLG5ZDQrKrP8EM") ).andDo( print() ).andExpect( status().isOk() );
}
}
결과
MockHttpServletRequest:
HTTP Method = POST
Request URI = /v1/signin
Parameters = {id=[1001], password=[password]}
Headers = []
Body = null
Session Attrs = {}
Handler:
Type = com.rest.api.controller.v1.SignController
Method = com.rest.api.controller.v1.SignController#signin(String, String)
Async:
Async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 200
Error message = null
Headers = [Content-Type:"application/json;charset=UTF-8"]
Content type = application/json
Body = {"success":true,"code":0,"msg":"성공하였습니다.","data":"eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMDAxIiwicm9sZXMiOltdLCJpYXQiOjE2MTA4MTEzOTEsImV4cCI6MTYxMDg5Nzc5MX0.GmiKem1NtHIuZwV0zPBB5v25SIaT9WKPzKIUXvzqE1Y"}
Forwarded URL = null
Redirected URL = null
Cookies = []
MockHttpServletRequest:
HTTP Method = GET
Request URI = /v1/users
Parameters = {lang=[ko]}
Headers = [X-AUTH-TOKEN:"eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMDAxIiwicm9sZXMiOltdLCJpYXQiOjE2MTA4MTA2NDEsImV4cCI6MTYxMDg5NzA0MX0.5rHP_aEyJk0wpozI1Azy1LAJZzIx0ZLG5ZDQrKrP8EM"]
Body = null
Session Attrs = {org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE=ko}
Handler:
Type = com.rest.api.controller.v1.UserController
Method = com.rest.api.controller.v1.UserController#findAllUser()
Async:
Async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 200
Error message = null
Headers = [Content-Type:"application/json;charset=UTF-8"]
Content type = application/json
Body = {"success":true,"code":0,"msg":"성공하였습니다.","list":[]}
Forwarded URL = null
Redirected URL = null
Cookies = []
Process finished with exit code 0
728x90
'Spring Boot Framework' 카테고리의 다른 글
[Spring Boot] Spring Resttemplate 예외 처리 (0) | 2021.01.31 |
---|---|
[Spring Boot] Spring Resttemplate Sample (0) | 2021.01.31 |
[Spring Boot] Spring Boot + JWT ( JSON Web Token ) + DB 연결 (0) | 2021.01.14 |
[Spring Boot] Spring Boot + JWT ( JSON Web Token ) 테스트 (0) | 2021.01.10 |
[Spring Boot] Spring Boot Sample REST + Log4J2 (0) | 2021.01.09 |