티스토리 뷰

IT/Spring

WebSquare + Spring MVC Test Code

underbell 2016. 10. 5. 10:43

UI 플랫폼으로 WebSquare 사용 시 Test Code


WebSquare View 단에서 Submisson 시 dataList 또는 dataMap 의 key 값을 Test Code에서 임의로 생성하여 Test 진행

Submisson 의 ref: 'data:json,' 으로 처리하므로 Gson 혹인 Jackson을 이용하여 Json String으로 변환하여 post로 전달


인코딩 설정 과 WebSquare 설정 파일 경로 설정 추가


package kr.co.cwit.common.security;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.HashMap;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
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.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import com.fasterxml.jackson.databind.ObjectMapper;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"file:src/test/resources/spring/*-context.xml", "file:src/test/resources/mybatis/*-context.xml"})
@WebAppConfiguration
public class LoginTest {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@Test
public void testConroller() throws Exception {
HashMap<String, Object> objMap = new HashMap<>();
HashMap<String, String> tempMap = new HashMap<>();
tempMap.put("USERID", "test");
tempMap.put("USERPW", "test");
objMap.put("dma_login", tempMap);
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(objMap);
mockMvc.perform(post("/loginProcess.do").contentType(MediaType.APPLICATION_JSON).content(json).accept(MediaType.parseMediaType("application/json;charset=UTF-8")))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json"))
.andDo(print());
}
}
view raw LoginTest.java hosted with ❤ by GitHub
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<argLine>-Dfile.encoding=UTF-8 -DWEBSQUARE_HOME=${websquare_home}</argLine>
<skipTests>${skipTests}</skipTests>
</configuration>
</plugin>
view raw pom.xml hosted with ❤ by GitHub


댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
TAG
more
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
글 보관함