[SampleDaoCase.java]
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import java.util.List;
import javax.annotation.Resource;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath*:config/spring/context-*.xml"})
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class SampleDaoCase {
private Logger log = LoggerFactory.getLogger(SampleDaoCase.class);
@Resource SampleDao sampleDao;
private String name = "이름 테스트";
@Test
public void seq01_addSample_글_등록() {
Sample sampleVo = new Sample();
sampleVo.setName(name);
sampleVo.setEmail("ddakkerABC@gmail.com");
int insertCnt = sampleDao.addSample(sampleVo);
log.debug("insertCnt: {}", insertCnt);
assertThat("글 등록 갯수", 1, is(insertCnt));
}
@Test
public void seq02_getSamplesXml_조회() {
log.debug("sampleDao: " + sampleDao);
Sample sampleVo = new Sample();
sampleVo.setEmail("ddakker@gmail.com");
List list = sampleDao.getSamplesXml(sampleVo);
log.debug("list: {}", list);
log.debug("seq: {}", list.get(0).getSeq());
log.debug("name: {}", list.get(0).getName());
if( list.get(0).getName() == null ) log.debug("name NULL");
else log.debug("name NOT NULL: {}", list.get(0).getName().length());
assertThat("리스트 갯수 체크", 0, not(list.size()));
assertThat("리스트 첫번째 배열의 네임값 체크", list.get(0).getName(), not(nullValue()));
assertThat("리스트 첫번째 배열의 네임값 체크 다른 방법", list.get(0),hasProperty("name", not(nullValue())));
}
@Test
public void seq03_getSamplesXml_조회_검색() {
log.debug("sampleDao: " + sampleDao);
Sample sampleVo = new Sample();
sampleVo.setEmail("ddakker@gmail.com");
sampleVo.setSeq(30);
List list = sampleDao.getSamplesXml(sampleVo);
log.debug("list: {}", list);
log.debug("seq: {}", list.get(0).getSeq());
log.debug("name: {}", list.get(0).getName());
if( list.get(0).getName() == null ) log.debug("name NULL");
else log.debug("name NOT NULL: {}", list.get(0).getName().length());
assertThat("리스트 갯수 체크", 0, not(list.size()));
assertThat("리스트 첫번째 배열의 네임값 체크", list.get(0).getName(), not(nullValue()));
assertThat("리스트 첫번째 배열의 네임값 체크 다른 방법", list.get(0),hasProperty("name", not(nullValue())));
}
@Test
public void seq04_deleteSample_삭제() {
Sample sampleVo = new Sample();
sampleVo.setName(name);
Sample sample = sampleDao.getSample(sampleVo);
assertThat("글", sample, not(nullValue()));
assertThat("글번호", sample.getSeq(), not(nullValue()));
sampleVo = new Sample();
sampleVo.setSeq(sample.getSeq());
int deleteCnt = sampleDao.deleteSample(sampleVo);
assertThat("글 삭제 갯수", 1, is(deleteCnt));
}
}
[SampleControllerCase.java]
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
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.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextHierarchy;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import pe.kr.ddakker.core.support.web.tags.pagination.Paging;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextHierarchy({
@ContextConfiguration("classpath*:config/spring/context-*.xml"),
@ContextConfiguration("file:webapp/WEB-INF/config/springmvc/name-servlet.xml")
})
public class SampleControllerCase {
private Logger log = LoggerFactory.getLogger(SampleControllerCase.class);
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@Test
public void listCase_페이징_리스트() throws Exception{
System.out.println("mockMvc1: " + mockMvc);
MvcResult mvcResult = this.mockMvc.perform(get("/sample/list").param("a", "1"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(model().attributeExists("paging"))
.andExpect(view().name("sample/list"))
.andReturn();
Paging samplePaging = (Paging) mvcResult.getModelAndView().getModelMap().get("paging");
assertThat("게시물 정상 여부", samplePaging, not(nullValue()));
assertThat("게시물 리스트 정상 여부", samplePaging.getList(), not(nullValue()));
if( samplePaging.getList().size() > 0 ){
log.debug("seq={}, name={}", samplePaging.getList().get(0).getSeq(), samplePaging.getList().get(0).getName());
assertThat("리스트 첫번째 배열의 네임값 체크 다른 방법", samplePaging.getList().get(0),hasProperty("name", not(nullValue())));
}
}
@Test
public void listCase_페이징_리스트_JSON() throws Exception{
System.out.println("mockMvc1: " + mockMvc);
this.mockMvc.perform(get("/sample/list").accept(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.result").value("0000"))
;
}
}
dependencies {
compile 'org.mybatis:mybatis:3.2.7'
compile 'org.mybatis:mybatis-spring:1.2.2'
testCompile 'junit:junit:4.11'
testCompile 'org.hamcrest:hamcrest-all:1.3'
testCompile 'com.jayway.jsonpath:json-path-assert:0.9.1'
testCompile 'org.springframework:spring-test:3.2.9.RELEASE'
}