Skip to content

Commit

Permalink
#16 - 로그인 컨트롤러 테스트 + 시큐리티 설정을 컨트롤러 테스트에 반영
Browse files Browse the repository at this point in the history
스프링 시큐리티가 자동 생성해준 `/login` 컨트롤러 테스트, 그리고 
시큐리티 설정을 부를 수 있도록 `@import` 추가
  • Loading branch information
rhakdnj committed Aug 8, 2022
1 parent 2f12c8f commit 037f054
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
@@ -1,17 +1,20 @@
package com.example.boardproject.controller;

import com.example.boardproject.config.SecurityConfig;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
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.context.annotation.Import;
import org.springframework.http.MediaType;
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.*;

@DisplayName("View 컨트롤러 - 게시글")
@Import(SecurityConfig.class)
@WebMvcTest(ArticleController.class)
class ArticleControllerTest {

Expand All @@ -36,7 +39,6 @@ public void givenNothing_whenRequestingArticlesView_thenReturnsArticlesView() th
.andExpect(model().attributeExists("articles"));
}

@Disabled("구현 중")
@DisplayName("[view][GET] 게시글 상세 페이지 - 정상 호출")
@Test
public void givenNothing_whenRequestingArticleView_thenReturnsArticleView() throws Exception {
Expand Down
@@ -0,0 +1,40 @@
package com.example.boardproject.controller;

import com.example.boardproject.config.SecurityConfig;
import org.junit.jupiter.api.DisplayName;
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.context.annotation.Import;
import org.springframework.http.MediaType;
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;

@DisplayName("View 컨트롤러 - 인증")
@Import(SecurityConfig.class)
@WebMvcTest
public class AuthControllerTest {

private final MockMvc mvc;

// 슬라이스 테스트에서는 생성자를 통한 주입이여도 @Autowired 해줘야함
// 즉, spring 이 생성자 주입할 때도 저절로 해주는 것
public AuthControllerTest(@Autowired MockMvc mvc) {
this.mvc = mvc;
}

@DisplayName("[view][GET] 로그인 페이지 - 정상 호출")
@Test
void givenNothing_whenTryingToLogin_thenReturnsLoginView() throws Exception {
// Given

// When & Then
mvc.perform(get("/login"))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.TEXT_HTML));
}

}

0 comments on commit 037f054

Please sign in to comment.