SpringBoot2로 Rest api 만들기(9) – Unit Test
This commit is contained in:
@@ -10,7 +10,7 @@ import org.springframework.boot.test.context.SpringBootTest;
|
|||||||
import org.springframework.security.test.context.support.WithMockUser;
|
import org.springframework.security.test.context.support.WithMockUser;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
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.test.web.servlet.request.MockMvcRequestBuilders;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.util.LinkedMultiValueMap;
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
@@ -36,15 +36,16 @@ public class UserControllerTest {
|
|||||||
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
|
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
|
||||||
params.add("id", "happydaddy@naver.com");
|
params.add("id", "happydaddy@naver.com");
|
||||||
params.add("password", "1234");
|
params.add("password", "1234");
|
||||||
ResultActions result = mockMvc.perform(post("/v1/signin").params(params))
|
MvcResult result = mockMvc.perform(post("/v1/signin").params(params))
|
||||||
.andDo(print())
|
.andDo(print())
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
.andExpect(jsonPath("$.success").value(true))
|
.andExpect(jsonPath("$.success").value(true))
|
||||||
.andExpect(jsonPath("$.code").value(0))
|
.andExpect(jsonPath("$.code").value(0))
|
||||||
.andExpect(jsonPath("$.msg").exists())
|
.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();
|
JacksonJsonParser jsonParser = new JacksonJsonParser();
|
||||||
token = jsonParser.parseMap(resultString).get("data").toString();
|
token = jsonParser.parseMap(resultString).get("data").toString();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,8 +12,7 @@ import java.util.Collections;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.is;
|
import static org.hamcrest.Matchers.is;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.*;
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@DataJpaTest
|
@DataJpaTest
|
||||||
@@ -39,8 +38,9 @@ public class UserJpaRepoTest {
|
|||||||
// when
|
// when
|
||||||
Optional<User> user = userJpaRepo.findByUid(uid);
|
Optional<User> user = userJpaRepo.findByUid(uid);
|
||||||
// then
|
// then
|
||||||
assertTrue(user.isPresent());
|
assertNotNull(user);// user객체가 null이 아닌지 체크
|
||||||
assertThat(user.get().getName(), is(name));
|
assertTrue(user.isPresent()); // user객체가 존재여부 true/false 체크
|
||||||
|
assertEquals(user.get().getName(), name); // user객체의 name과 name변수 값이 같은지 체크
|
||||||
|
assertThat(user.get().getName(), is(name)); // user객체의 name과 name변수 값이 같은지 체크
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user