Skip to content

Commit

Permalink
test: 게시글 조회 MVC Test 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
enjay27 committed Jun 11, 2021
1 parent b202156 commit 0ecb5a7
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/test/java/com/devin/dev/mvc/PostControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.devin.dev.mvc;

import com.devin.dev.entity.post.Post;
import com.devin.dev.repository.post.PostRepository;
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.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import javax.servlet.http.HttpServletRequestWrapper;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;
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.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;

@SpringBootTest
@AutoConfigureMockMvc
class PostControllerTest {

@Autowired
private MockMvc mvc;

@Autowired
private PostRepository postRepository;

@Test
@WithMockUser
void requestPostSucceeded() throws Exception {
List<Post> posts = postRepository.findByTitle("title_1");
mvc.perform(get("/postlist/" + posts.get(0).getId()))
.andExpect(status().isOk())
.andDo(print());
}

@Test
@WithMockUser
void requestPostFailed() throws Exception {
mvc.perform(get("/postlist/0"))
.andExpect(status().is4xxClientError())
.andDo(print());
}
}

0 comments on commit 0ecb5a7

Please sign in to comment.