헤르메스 LIFE

[Spring Boot] JUnit Test 본문

Spring Boot Framework

[Spring Boot] JUnit Test

헤르메스의날개 2021. 1. 17. 00:46
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