Skip to content

Commit

Permalink
#4 test: 정상적으로 글이 들어오는 컨트롤러 테스트
Browse files Browse the repository at this point in the history
  • Loading branch information
shoeone96 committed Nov 16, 2023
1 parent 0887c1a commit 4ea0c15
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/test/java/com/example/board/controller/PostControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.example.board.controller;

import com.example.board.dto.PostDto;
import com.fasterxml.jackson.databind.ObjectMapper;
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.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;

import static org.junit.jupiter.api.Assertions.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@SpringBootTest
@AutoConfigureMockMvc
class PostControllerTest {

@MockBean
PostController postController;

@Autowired
MockMvc mockMvc;

@Autowired
ObjectMapper objectMapper;

@Test
@DisplayName("정상적으로 게시글을 등록하는 상황")
void postSuccess() throws Exception {
//given
PostDto postDto = new PostDto(1, "test", "test Contents");

//when, then
mockMvc.perform(post("http://localhost:8080/api/v1/posts")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsBytes(postDto)))
.andExpect(status().isOk())
.andDo(print());
}

}

0 comments on commit 4ea0c15

Please sign in to comment.