SpringBoot2로 Rest api 만들기(9) – Unit Test

This commit is contained in:
kimyonghwa
2019-04-18 15:20:56 +09:00
parent e4d5cf3a77
commit d2a6c9bc81
2 changed files with 10 additions and 9 deletions

View File

@@ -10,7 +10,7 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.LinkedMultiValueMap;
@@ -36,15 +36,16 @@ public class UserControllerTest {
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("id", "happydaddy@naver.com");
params.add("password", "1234");
ResultActions result = mockMvc.perform(post("/v1/signin").params(params))
MvcResult result = mockMvc.perform(post("/v1/signin").params(params))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.success").value(true))
.andExpect(jsonPath("$.code").value(0))
.andExpect(jsonPath("$.msg").exists())
.andExpect(jsonPath("$.data").exists());
.andExpect(jsonPath("$.data").exists())
.andReturn();
String resultString = result.andReturn().getResponse().getContentAsString();
String resultString = result.getResponse().getContentAsString();
JacksonJsonParser jsonParser = new JacksonJsonParser();
token = jsonParser.parseMap(resultString).get("data").toString();
}

View File

@@ -12,8 +12,7 @@ import java.util.Collections;
import java.util.Optional;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
@RunWith(SpringRunner.class)
@DataJpaTest
@@ -39,8 +38,9 @@ public class UserJpaRepoTest {
// when
Optional<User> user = userJpaRepo.findByUid(uid);
// then
assertTrue(user.isPresent());
assertThat(user.get().getName(), is(name));
assertNotNull(user);// user객체가 null이 아닌지 체크
assertTrue(user.isPresent()); // user객체가 존재여부 true/false 체크
assertEquals(user.get().getName(), name); // user객체의 name과 name변수 값이 같은지 체크
assertThat(user.get().getName(), is(name)); // user객체의 name과 name변수 값이 같은지 체크
}
}