전체를 돌리니까 해당 부분에서만 터진다.
package shop.mtcoding.bank.web;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.jupiter.api.BeforeEach;
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.context.SpringBootTest.WebEnvironment;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import com.fasterxml.jackson.databind.ObjectMapper;
import shop.mtcoding.bank.config.dummy.DummyObject;
import shop.mtcoding.bank.domain.user.UserRepository;
import shop.mtcoding.bank.dto.user.UserRequestDto.JoinRequestDto;
@AutoConfigureMockMvc
@SpringBootTest(webEnvironment = WebEnvironment.MOCK)
public class UserControllerTest extends DummyObject {
@Autowired
private MockMvc mvc;
@Autowired
private ObjectMapper om;
@Autowired
private UserRepository userRepository;
@BeforeEach
public void setUp() {
dataSetting();
}
@Test
public void join_success_test() throws Exception {
// given
JoinRequestDto joinRequestDto = new JoinRequestDto();
joinRequestDto.setUsername("love");
joinRequestDto.setPassword("1234");
joinRequestDto.setEmail("love@nate.com");
joinRequestDto.setFullname("러브");
String requestBody = om.writeValueAsString(joinRequestDto);
System.out.println("테스트 : " + requestBody);
// when
ResultActions resultActions = mvc.perform(post("/api/join")
.content(requestBody)
.contentType(MediaType.APPLICATION_JSON));
String responseBody = resultActions.andReturn().getResponse().getContentAsString();
System.out.println("테스트 : " + responseBody);
// then
resultActions.andExpect(status().isCreated());
}
@Test
public void join_fail_test() throws Exception {
// given
JoinRequestDto joinRequestDto = new JoinRequestDto();
joinRequestDto.setUsername("ssar");
joinRequestDto.setPassword("1234");
joinRequestDto.setEmail("ssar@nate.com");
joinRequestDto.setFullname("쌀");
String requestBody = om.writeValueAsString(joinRequestDto);
System.out.println("테스트 : " + requestBody);
// when
ResultActions resultActions = mvc.perform(post("/api/join")
.content(requestBody)
.contentType(MediaType.APPLICATION_JSON));
String responseBody = resultActions.andReturn().getResponse().getContentAsString();
System.out.println("테스트 : " + responseBody);
// then
resultActions.andExpect(status().isBadRequest());
}
private void dataSetting() {
userRepository.save(newUser("ssar", "쌀"));
}
}
이유는 롤백이 되지않아서이다. 따라서 @Transaction을 추가해주자
package shop.mtcoding.bank.web;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.jupiter.api.BeforeEach;
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.context.SpringBootTest.WebEnvironment;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.transaction.annotation.Transactional;
import com.fasterxml.jackson.databind.ObjectMapper;
import shop.mtcoding.bank.config.dummy.DummyObject;
import shop.mtcoding.bank.domain.user.UserRepository;
import shop.mtcoding.bank.dto.user.UserRequestDto.JoinRequestDto;
@Transactional
@AutoConfigureMockMvc
@SpringBootTest(webEnvironment = WebEnvironment.MOCK)
public class UserControllerTest extends DummyObject {
@Autowired
private MockMvc mvc;
@Autowired
private ObjectMapper om;
@Autowired
private UserRepository userRepository;
@BeforeEach
public void setUp() {
userRepository.save(newUser("ssar", "쌀"));
}
@Test
public void join_success_test() throws Exception {
// given
JoinRequestDto joinRequestDto = new JoinRequestDto();
joinRequestDto.setUsername("love");
joinRequestDto.setPassword("1234");
joinRequestDto.setEmail("love@nate.com");
joinRequestDto.setFullname("러브");
String requestBody = om.writeValueAsString(joinRequestDto);
System.out.println("테스트 : " + requestBody);
// when
ResultActions resultActions = mvc.perform(post("/api/join")
.content(requestBody)
.contentType(MediaType.APPLICATION_JSON));
String responseBody = resultActions.andReturn().getResponse().getContentAsString();
System.out.println("테스트 : " + responseBody);
// then
resultActions.andExpect(status().isCreated());
}
@Test
public void join_fail_test() throws Exception {
// given
JoinRequestDto joinRequestDto = new JoinRequestDto();
joinRequestDto.setUsername("ssar");
joinRequestDto.setPassword("1234");
joinRequestDto.setEmail("ssar@nate.com");
joinRequestDto.setFullname("쌀");
String requestBody = om.writeValueAsString(joinRequestDto);
System.out.println("테스트 : " + requestBody);
// when
ResultActions resultActions = mvc.perform(post("/api/join")
.content(requestBody)
.contentType(MediaType.APPLICATION_JSON));
String responseBody = resultActions.andReturn().getResponse().getContentAsString();
System.out.println("테스트 : " + responseBody);
// then
resultActions.andExpect(status().isBadRequest());
}
}