SlideShare ist ein Scribd-Unternehmen logo
1 von 42
SPRING TEST MVC
FRAMEWORK
MVC 테스트 작성과 Test API
소개
KSUG 이수홍
기존의 Web Application Test?
1. 로컬 서버를 실행한다.
2. 실행할 때까지 멍하게 있는다.
3. 웹브라우저를 띄운다.
4. 웹브라우저 테스트할 URI를
입력한다.
…
무한 반복 ∞
물론!
Mock 객체를 이용한 코드 테스트
MockHttpServletRequest req= new MockHttpServletRequest();
MockHttpServletResponse res = new
MockHttpServletResponse();
req.setParameter(“id”, “1”);
…
TestController test = new TestController();
test.handleRequest(req, res);
하지만 Mock 객체로 테스트는
Spring MVC에서는 한계가 존재 …
앗! Request 객체가 어디 갔지?
@RequestMapping(value=”/main", method=RequestMethod.GET)
public String main(@RequestParam(”id") String id) {
…
return “main”;
}
“그럼 그냥 메소드 호출 해서 테스트
하면 되는것 아닌가?”
// TestController.java
@RequestMapping(value=”/main", method=RequestMethod.GET)
public String main(@RequestParam(”id") String id) {
…
return “main”;
}
// Test
@Test
public void test() throws Exception {
TestController controller = new TestController();
String result = controller.main(“spring83”);
log.debug(“result : {}“,result);
assertEqual(result, “main”);
}
// console
…. result : main
테스트는 통과
…
하지만
…
원하는 결과??
Interceptor가 실행 후 변경된 값을
알고싶다면?
Model에 어떤 값이 있는지 알고싶다면?
어떤 View를 선택했는지 알고싶다면?
현재 URL이 어떤 Controller의 Method를
호출하는지 알고싶다면?
…
…
Spring MVC의 다양한 기능
테스트하기에는 “태”부족
그래서 선택한
Spring Test MVC
…
Module
기존 코드 테스트 방식
Test
Engine
call
Methodcall
call
Class
Spring Test MVC 방식
Test
Client
Request
MockMVC
Response
Spring MVC
실제 WAS에서 Spring MVC가
요청(request)에
응답(response)하는 부분을
Test에서
완벽히 재현!!
Client
HttpServletRequest
HttpServletResponse
WAS
Test
Mock Request
Mock Response
MockMVC
응답과 요청이 동일!!
예제 코드1
@RunWith(SpringJUnit4ClassRunner.class)
• @ContextConfiguration(classes=AppConfig.class)
• @WebAppConfiguration
• public class ControllerTest {
• @Autowired
• WebApplicationContext wac;
• MockMvc mockMvc;
• @Before
• public void init() {
• mockMvc =
MockMvcBuilders.webAppContextSetup(wac).build();
• }
예제코드2
• @Test
• public void test1() throws Exception {
• mockMvc.perform(get("/test/")
• .header("x-requested-with", "XMLHttpRequest")
• .accept(MediaType.APPLICATION_JSON))
• .andExpect(handler().handlerType(Controller.class))
• .andExpect(handler().methodName("test"))
• .andDo(MockMvcResultHandlers.print());
• }
• }
코드 설명 (설정 부분)
• @WebAppConfiguration
• WebApplicationContext 를 생성 할 수 있도록 하는 어노테이션
• @Autowired WebApplicationContext wac;
• MockMvc객체를 생성 하기 위해 WebApplicationContext가 필요
• MockMvc mockMvc;
• mockMVC 재사용
• mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
• Test를 위한 Spring MVC 엔진을 탑제한(?) MockMVC객체를
생성
예제 코드 설명(request)
• mockMvc.perform(get("/test/”)
• get => http method 설정 post, delete, put 등 사용 가능
• “/test/” => 호출 URI를 기술 한다.
• .accept(MediaType.APPLICATION_JSON))
• accept 정보를 설정
• .header("x-requested-with", "XMLHttpRequest")
• Custom Header 정보를 설정
• 참고 소스에 기술 되지 않은 요청 설정에 대한 주요 기능
• .param(“key”, “value”) // 파라메터 설정
• .cookie(new Cookie(“key”,”value”)) // 쿠키설정
• .sessionAttr(“key”, “value”) // 세션 설정
• …
예제 코드 설명(response)
• .andExpect(handler().handlerType(Controller.class))
• 요청에대한 맵핑된 MVC컨트롤러 클래스 확인
• .andExpect(handler().methodName("test"))
• 요청에대한 맵핑된 MVC컨트롤러의 클래스에 속한 메소드 확인
• .andDo(MockMvcResultHandlers.print());
• 해당 요청과 응답에 대한 처리를 하는 부분
• MockMvcResultHandlers.print()
• Spring Test MVC 응답 결과에 대한 모든 정보 출력 처리
Spring Test MVC 구조
Perform => 요청 사전조건
Expect => 응답 관련 테스트
Do => 테스트시 직접 실행
Return => 테스트 결과 반환
(참조)
org.springframework.test.web.servlet
.result.MockMvcResultMatchers
도대체 어떤 테스트를
지원하는가?
MockMvcResult
Matchers
종류
Request Test Handler Test
Status Test
Header Test
Model Test
View Test
Content Test
ForwardUrl Test
RedirectUrl Test
Flash attribute Test
Http Servlet 관련 Test Spring MVC 관련 Test
JsonPath Test
XPath Test
Content Data Test
• @Test
• public void jsonPathTest() throws Exception {
• mockMvc.perform(get("/test/")
• .header("x-requested-with", "XMLHttpRequest")
• .accept(MediaType.APPLICATION_JSON))
• .andExpect(handler().handlerType(ApiController.class))
• .andExpect(handler().methodName(”getJson"))
• .andExpect(jsonPath("$.status").value("FAIL”))
• .andDo(MockMvcResultHandlers.print());
• }
• }
JSONPath Test 예제
JSONPath 사용 하기위해는?
• JSONPath의 사용방법 필요
• 참조 : http://goessner.net/articles/JsonPath/
• com.jayway.jsonpath 라이브러리 필요
추가적인 질문?!
Servlet filter는 어떻게 테스트 하나요!?
Spring security는 어떻게 테스트하나요?
Spring Test MVC로 가능한가요?
…
@RunWith(SpringJUnit4ClassRunner.class)
• @ContextConfiguration(classes=WebAppConfig.class)
• @WebAppConfiguration
• public class ControllerTest {
• @Autowired
• FilterChainProxy springSecurityFilterChain;
• @Autowired
• WebApplicationContext wac;
• MockMvc mockMvc;
• @Before
• public void init() {
• mockMvc = MockMvcBuilders.webAppContextSetup(wac)
• .addFilters(springSecurityFilterChain).build();
• }
Spring security 연동 예제
Spring security 연동 시 참고
• 참조 소스
• 참조 : https://github.com/SpringSource/spring-test-
mvc/blob/master/src/test/java/org/springframework/test/web/server/
samples/context/SpringSecurityTests.java
• Spring security filter가 설정된 MockMvc객체를 사용시
모든 요청이 권한과 관계가 맺어지기 때문에 신경써서
Test작성이 필요
…
@Before
• public void init() {
• mockMvc = MockMvcBuilders.webAppContextSetup(wac)
• .alwaysDo(MockMvcResultHandlers.print())
• // 모든 테스트 정보를 logging을 한다.
• .alwaysExpect(status().isOk())
• // 모든 테스트는 http status 코드가 200
이어야한다.
• .build();
• }
마지막 팁!
• 공통적인 테스트 및 실행할 코드를 선언하는 방법
• 하단 예제 참조
감사합니다.
부록
Request test 주요 메소드 요약
• Request attribute test ( Attribute 정보 확인 테스트 )
• .andExpect(request().attribute(.., ..))
• Session attribute test ( 세션 정보 확인 테스트 )
• .andExpect(request().sessionAttribute(.., ..))
• Async test ( Servlet 3.0, Spring 3.2 부터 지원 하는 비동기
요청 테스트 )
• .andExpect(request(). asyncStarted(.., ..))
• 비동기요청인가?
• .andExpect(request(). asyncNotStarted(.., ..))
• 비동기요청이 아닌가?
• .andExpect(request(). asyncResult(.., ..))
• 비동기 요청에 대한 응답 정보는?
Handler test 주요 메소드 요약
• Handler type test ( 요청에 맵핑 되는 Handler 타입을
테스트 )
• .andExpect(handler().handlerType(..))
• Handler method name test ( 요청에 맵핑되는 Handler
메소드명 테스트 )
• .andExpect(handler().methodName(..))
• Handler method type test ( 요청에 맵핑되는 Handler
메소드타입 테스트)
• .andExpect(handler().method(..))
Model test 주요 메소드 요약
• Model attrbute test ( Model 속성 테스트 )
• .andExpect(model().attribute(..))
• .andExpect(model().attributeExists(..))
• .andExpect(model().attributeErrorCount(..))
• .andExpect(model().attributeHasErrors(..))
• .andExpect(model().attributeHasNoErrors(..))
• .andExpect(model().attributeHasFieldErrors(..))
• .andExpect(model().errorCount(..))
• .andExpect(model().hasErrors(..))
• .andExpect(model().hasNoErrors(..))
• 일단 일일이 설명 보다는 메소드명에 모든 설명이 담겨있다!! ( 귀찮아서
그런거 절대 아님! )
• 대략 해당 Model에 들어 이는 속성에 대한 값과 Validation체크로 통한
에러 여부 등을 체크한다.
View test 주요 메소드 요약
• View name test ( 해당 요청에 선택된 View name 테스트 )
• .andExpect(view().name(..))
Status, header test 주요 메소드 요약
• Status test ( 응답 상태에 대한 테스트 )
• .andExpect(status().is(..))
• 응답상태 코드에 대한 테스트 ( 200:정상 … )
• org.springframework.http HttpStatus 참조
• Error message test ( 에러메세지에 대한 테스트 )
• .andExpect(status().reason(..))
• 메세지 문자열 등 테스트 한다.
• Header test ( 헤더 정보에 대한 테스트 )
• .andExpect(header().string(..))
• .andExpect(header().longValue(..))
Content Test 주요 메소드 요약
• ContentType test( contentType 테스트 )
• .andExpect(content().contentType(..))
• .andExpect(content().contentTypeCompatibleWith(..))
• Encoding test ( content 문자열 인코딩 테스트 )
• .andExpect(content().encoding(..))
• Content test ( content 테스트 )
• .andExpect(content().string(..)) : content 문자열 비교
• .andExpect(content().bytes(..)) : content byte 비교
• .andExpect(content().xml(..)) : content xml dom 비교
• .andExpect(content().node(..)) : content xml dom node 비교
• .andExpect(content().source(..)) : content xml dom source 객체 비교
Other Test 주요 메소드 요약
• Forwarded url test( 포워딩 URL 테스트 )
• .andExpect(forwardedUrl(..))
• Redirected url test ( 리다이렉트 URL 테스트 )
• .andExpect(redirectedUrl(..))
• Flash attribute test ( Spring 3.1 부터 지원 되는 Flash
attribute값 테스트 )
• .andExpect(flash().attribute(..)) : attribute 테스트
• .andExpect(flash(). attributeExists(..)) : attribute 존재 여부 테스트
• .andExpect(flash(). attributeCount(..)) : attribute의 수 테스트
끝

Weitere ähnliche Inhalte

Was ist angesagt?

Basics of MongoDB
Basics of MongoDB Basics of MongoDB
Basics of MongoDB Habilelabs
 
오픈소스GIS의 이해와 활용
오픈소스GIS의 이해와 활용오픈소스GIS의 이해와 활용
오픈소스GIS의 이해와 활용SANGHEE SHIN
 
오픈소스 GIS의 이해 - OSgeo Projects 중심
오픈소스 GIS의 이해 - OSgeo Projects 중심오픈소스 GIS의 이해 - OSgeo Projects 중심
오픈소스 GIS의 이해 - OSgeo Projects 중심MinPa Lee
 
Nest.js Introduction
Nest.js IntroductionNest.js Introduction
Nest.js IntroductionTakuya Tejima
 
도메인 주도 설계 - 16 대규모 구조
도메인 주도 설계 - 16 대규모 구조도메인 주도 설계 - 16 대규모 구조
도메인 주도 설계 - 16 대규모 구조SH Park
 
QGIS 소개 및 ArcMap과의 비교
QGIS 소개 및 ArcMap과의 비교QGIS 소개 및 ArcMap과의 비교
QGIS 소개 및 ArcMap과의 비교BJ Jang
 
Use Node.js to create a REST API
Use Node.js to create a REST APIUse Node.js to create a REST API
Use Node.js to create a REST APIFabien Vauchelles
 
Mongo DB 성능최적화 전략
Mongo DB 성능최적화 전략Mongo DB 성능최적화 전략
Mongo DB 성능최적화 전략Jin wook
 
Introduction to NOSQL databases
Introduction to NOSQL databasesIntroduction to NOSQL databases
Introduction to NOSQL databasesAshwani Kumar
 
Realtime vs Cloud Firestore
Realtime vs Cloud Firestore Realtime vs Cloud Firestore
Realtime vs Cloud Firestore Appinventiv
 
테스트 가능한 소프트웨어 설계와 TDD작성 패턴 (Testable design and TDD)
테스트 가능한 소프트웨어 설계와 TDD작성 패턴 (Testable design and TDD)테스트 가능한 소프트웨어 설계와 TDD작성 패턴 (Testable design and TDD)
테스트 가능한 소프트웨어 설계와 TDD작성 패턴 (Testable design and TDD)Suwon Chae
 
Introduction to react-query. A Redux alternative? (Nikos Kleidis, Front End D...
Introduction to react-query. A Redux alternative? (Nikos Kleidis, Front End D...Introduction to react-query. A Redux alternative? (Nikos Kleidis, Front End D...
Introduction to react-query. A Redux alternative? (Nikos Kleidis, Front End D...GreeceJS
 
오픈소스 GIS 개요
오픈소스 GIS 개요오픈소스 GIS 개요
오픈소스 GIS 개요slhead1
 
MongoDB Internals
MongoDB InternalsMongoDB Internals
MongoDB InternalsSiraj Memon
 

Was ist angesagt? (20)

Basics of MongoDB
Basics of MongoDB Basics of MongoDB
Basics of MongoDB
 
오픈소스GIS의 이해와 활용
오픈소스GIS의 이해와 활용오픈소스GIS의 이해와 활용
오픈소스GIS의 이해와 활용
 
오픈소스 GIS의 이해 - OSgeo Projects 중심
오픈소스 GIS의 이해 - OSgeo Projects 중심오픈소스 GIS의 이해 - OSgeo Projects 중심
오픈소스 GIS의 이해 - OSgeo Projects 중심
 
Nest.js Introduction
Nest.js IntroductionNest.js Introduction
Nest.js Introduction
 
Node.js Basics
Node.js Basics Node.js Basics
Node.js Basics
 
도메인 주도 설계 - 16 대규모 구조
도메인 주도 설계 - 16 대규모 구조도메인 주도 설계 - 16 대규모 구조
도메인 주도 설계 - 16 대규모 구조
 
QGIS 소개 및 ArcMap과의 비교
QGIS 소개 및 ArcMap과의 비교QGIS 소개 및 ArcMap과의 비교
QGIS 소개 및 ArcMap과의 비교
 
Use Node.js to create a REST API
Use Node.js to create a REST APIUse Node.js to create a REST API
Use Node.js to create a REST API
 
Mongo DB 성능최적화 전략
Mongo DB 성능최적화 전략Mongo DB 성능최적화 전략
Mongo DB 성능최적화 전략
 
Microservices chassis
Microservices chassisMicroservices chassis
Microservices chassis
 
Introduction to NOSQL databases
Introduction to NOSQL databasesIntroduction to NOSQL databases
Introduction to NOSQL databases
 
React
React React
React
 
Realtime vs Cloud Firestore
Realtime vs Cloud Firestore Realtime vs Cloud Firestore
Realtime vs Cloud Firestore
 
Node.js
Node.jsNode.js
Node.js
 
테스트 가능한 소프트웨어 설계와 TDD작성 패턴 (Testable design and TDD)
테스트 가능한 소프트웨어 설계와 TDD작성 패턴 (Testable design and TDD)테스트 가능한 소프트웨어 설계와 TDD작성 패턴 (Testable design and TDD)
테스트 가능한 소프트웨어 설계와 TDD작성 패턴 (Testable design and TDD)
 
Jenkins Pipelines
Jenkins PipelinesJenkins Pipelines
Jenkins Pipelines
 
Introduction to react-query. A Redux alternative? (Nikos Kleidis, Front End D...
Introduction to react-query. A Redux alternative? (Nikos Kleidis, Front End D...Introduction to react-query. A Redux alternative? (Nikos Kleidis, Front End D...
Introduction to react-query. A Redux alternative? (Nikos Kleidis, Front End D...
 
오픈소스 GIS 개요
오픈소스 GIS 개요오픈소스 GIS 개요
오픈소스 GIS 개요
 
MongoDB
MongoDBMongoDB
MongoDB
 
MongoDB Internals
MongoDB InternalsMongoDB Internals
MongoDB Internals
 

Ähnlich wie Spring test mvc 발표자료

ParameterizedTest 와 ContextCaching.pptx
ParameterizedTest 와 ContextCaching.pptxParameterizedTest 와 ContextCaching.pptx
ParameterizedTest 와 ContextCaching.pptxjunu6
 
Spring MVC
Spring MVCSpring MVC
Spring MVCymtech
 
Okjsp 13주년 발표자료: 생존 프로그래밍 Test
Okjsp 13주년 발표자료: 생존 프로그래밍 TestOkjsp 13주년 발표자료: 생존 프로그래밍 Test
Okjsp 13주년 발표자료: 생존 프로그래밍 Testbeom kyun choi
 
Spring boot 공작소(1-4장)
Spring boot 공작소(1-4장)Spring boot 공작소(1-4장)
Spring boot 공작소(1-4장)Choonghyun Yang
 
04.실행환경 실습교재(화면처리)
04.실행환경 실습교재(화면처리)04.실행환경 실습교재(화면처리)
04.실행환경 실습교재(화면처리)Hankyo
 
Pinpoint spring_camp 2015
Pinpoint spring_camp 2015Pinpoint spring_camp 2015
Pinpoint spring_camp 2015Woonduk-Kang
 
Spring mvc
Spring mvcSpring mvc
Spring mvcksain
 
02.실행환경 실습교재(데이터처리)
02.실행환경 실습교재(데이터처리)02.실행환경 실습교재(데이터처리)
02.실행환경 실습교재(데이터처리)Hankyo
 
HeadFisrt Servlet&JSP Chapter 3
HeadFisrt Servlet&JSP Chapter 3HeadFisrt Servlet&JSP Chapter 3
HeadFisrt Servlet&JSP Chapter 3J B
 
Spring 3의 jsr 303 지원
Spring 3의 jsr 303 지원Spring 3의 jsr 303 지원
Spring 3의 jsr 303 지원Sewon Ann
 
Postman과 Newman을 이용한 RestAPI 테스트 자동화 가이드
Postman과 Newman을 이용한 RestAPI 테스트 자동화 가이드 Postman과 Newman을 이용한 RestAPI 테스트 자동화 가이드
Postman과 Newman을 이용한 RestAPI 테스트 자동화 가이드 SangIn Choung
 
Nodejs, PhantomJS, casperJs, YSlow, expressjs
Nodejs, PhantomJS, casperJs, YSlow, expressjsNodejs, PhantomJS, casperJs, YSlow, expressjs
Nodejs, PhantomJS, casperJs, YSlow, expressjs기동 이
 
Ji 개발 리뷰 (신림프로그래머)
Ji 개발 리뷰 (신림프로그래머)Ji 개발 리뷰 (신림프로그래머)
Ji 개발 리뷰 (신림프로그래머)beom kyun choi
 
Jdk(java) 7 - 5. invoke-dynamic
Jdk(java) 7 - 5. invoke-dynamicJdk(java) 7 - 5. invoke-dynamic
Jdk(java) 7 - 5. invoke-dynamicknight1128
 
Servlet design pattern
Servlet design patternServlet design pattern
Servlet design patternSukjin Yun
 

Ähnlich wie Spring test mvc 발표자료 (20)

Cygnus unit test
Cygnus unit testCygnus unit test
Cygnus unit test
 
ParameterizedTest 와 ContextCaching.pptx
ParameterizedTest 와 ContextCaching.pptxParameterizedTest 와 ContextCaching.pptx
ParameterizedTest 와 ContextCaching.pptx
 
Spring MVC
Spring MVCSpring MVC
Spring MVC
 
Okjsp 13주년 발표자료: 생존 프로그래밍 Test
Okjsp 13주년 발표자료: 생존 프로그래밍 TestOkjsp 13주년 발표자료: 생존 프로그래밍 Test
Okjsp 13주년 발표자료: 생존 프로그래밍 Test
 
Springmvc
SpringmvcSpringmvc
Springmvc
 
Spring boot 공작소(1-4장)
Spring boot 공작소(1-4장)Spring boot 공작소(1-4장)
Spring boot 공작소(1-4장)
 
4-3. jquery
4-3. jquery4-3. jquery
4-3. jquery
 
04.실행환경 실습교재(화면처리)
04.실행환경 실습교재(화면처리)04.실행환경 실습교재(화면처리)
04.실행환경 실습교재(화면처리)
 
Pinpoint spring_camp 2015
Pinpoint spring_camp 2015Pinpoint spring_camp 2015
Pinpoint spring_camp 2015
 
Spring mvc
Spring mvcSpring mvc
Spring mvc
 
02.실행환경 실습교재(데이터처리)
02.실행환경 실습교재(데이터처리)02.실행환경 실습교재(데이터처리)
02.실행환경 실습교재(데이터처리)
 
HeadFisrt Servlet&JSP Chapter 3
HeadFisrt Servlet&JSP Chapter 3HeadFisrt Servlet&JSP Chapter 3
HeadFisrt Servlet&JSP Chapter 3
 
Spring 3의 jsr 303 지원
Spring 3의 jsr 303 지원Spring 3의 jsr 303 지원
Spring 3의 jsr 303 지원
 
4-1. javascript
4-1. javascript4-1. javascript
4-1. javascript
 
Postman과 Newman을 이용한 RestAPI 테스트 자동화 가이드
Postman과 Newman을 이용한 RestAPI 테스트 자동화 가이드 Postman과 Newman을 이용한 RestAPI 테스트 자동화 가이드
Postman과 Newman을 이용한 RestAPI 테스트 자동화 가이드
 
Nodejs, PhantomJS, casperJs, YSlow, expressjs
Nodejs, PhantomJS, casperJs, YSlow, expressjsNodejs, PhantomJS, casperJs, YSlow, expressjs
Nodejs, PhantomJS, casperJs, YSlow, expressjs
 
Ji 개발 리뷰 (신림프로그래머)
Ji 개발 리뷰 (신림프로그래머)Ji 개발 리뷰 (신림프로그래머)
Ji 개발 리뷰 (신림프로그래머)
 
Jdk(java) 7 - 5. invoke-dynamic
Jdk(java) 7 - 5. invoke-dynamicJdk(java) 7 - 5. invoke-dynamic
Jdk(java) 7 - 5. invoke-dynamic
 
Html5 performance
Html5 performanceHtml5 performance
Html5 performance
 
Servlet design pattern
Servlet design patternServlet design pattern
Servlet design pattern
 

Spring test mvc 발표자료

  • 1. SPRING TEST MVC FRAMEWORK MVC 테스트 작성과 Test API 소개 KSUG 이수홍
  • 3. 1. 로컬 서버를 실행한다. 2. 실행할 때까지 멍하게 있는다. 3. 웹브라우저를 띄운다. 4. 웹브라우저 테스트할 URI를 입력한다. … 무한 반복 ∞
  • 5. Mock 객체를 이용한 코드 테스트 MockHttpServletRequest req= new MockHttpServletRequest(); MockHttpServletResponse res = new MockHttpServletResponse(); req.setParameter(“id”, “1”); … TestController test = new TestController(); test.handleRequest(req, res);
  • 6. 하지만 Mock 객체로 테스트는 Spring MVC에서는 한계가 존재 …
  • 7. 앗! Request 객체가 어디 갔지? @RequestMapping(value=”/main", method=RequestMethod.GET) public String main(@RequestParam(”id") String id) { … return “main”; }
  • 8. “그럼 그냥 메소드 호출 해서 테스트 하면 되는것 아닌가?”
  • 9. // TestController.java @RequestMapping(value=”/main", method=RequestMethod.GET) public String main(@RequestParam(”id") String id) { … return “main”; } // Test @Test public void test() throws Exception { TestController controller = new TestController(); String result = controller.main(“spring83”); log.debug(“result : {}“,result); assertEqual(result, “main”); } // console …. result : main
  • 11. Interceptor가 실행 후 변경된 값을 알고싶다면? Model에 어떤 값이 있는지 알고싶다면? 어떤 View를 선택했는지 알고싶다면? 현재 URL이 어떤 Controller의 Method를 호출하는지 알고싶다면? … …
  • 12. Spring MVC의 다양한 기능 테스트하기에는 “태”부족
  • 14. … Module 기존 코드 테스트 방식 Test Engine call Methodcall call Class
  • 15. Spring Test MVC 방식 Test Client Request MockMVC Response Spring MVC
  • 16. 실제 WAS에서 Spring MVC가 요청(request)에 응답(response)하는 부분을 Test에서 완벽히 재현!!
  • 18. 예제 코드1 @RunWith(SpringJUnit4ClassRunner.class) • @ContextConfiguration(classes=AppConfig.class) • @WebAppConfiguration • public class ControllerTest { • @Autowired • WebApplicationContext wac; • MockMvc mockMvc; • @Before • public void init() { • mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); • }
  • 19. 예제코드2 • @Test • public void test1() throws Exception { • mockMvc.perform(get("/test/") • .header("x-requested-with", "XMLHttpRequest") • .accept(MediaType.APPLICATION_JSON)) • .andExpect(handler().handlerType(Controller.class)) • .andExpect(handler().methodName("test")) • .andDo(MockMvcResultHandlers.print()); • } • }
  • 20. 코드 설명 (설정 부분) • @WebAppConfiguration • WebApplicationContext 를 생성 할 수 있도록 하는 어노테이션 • @Autowired WebApplicationContext wac; • MockMvc객체를 생성 하기 위해 WebApplicationContext가 필요 • MockMvc mockMvc; • mockMVC 재사용 • mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); • Test를 위한 Spring MVC 엔진을 탑제한(?) MockMVC객체를 생성
  • 21. 예제 코드 설명(request) • mockMvc.perform(get("/test/”) • get => http method 설정 post, delete, put 등 사용 가능 • “/test/” => 호출 URI를 기술 한다. • .accept(MediaType.APPLICATION_JSON)) • accept 정보를 설정 • .header("x-requested-with", "XMLHttpRequest") • Custom Header 정보를 설정 • 참고 소스에 기술 되지 않은 요청 설정에 대한 주요 기능 • .param(“key”, “value”) // 파라메터 설정 • .cookie(new Cookie(“key”,”value”)) // 쿠키설정 • .sessionAttr(“key”, “value”) // 세션 설정 • …
  • 22. 예제 코드 설명(response) • .andExpect(handler().handlerType(Controller.class)) • 요청에대한 맵핑된 MVC컨트롤러 클래스 확인 • .andExpect(handler().methodName("test")) • 요청에대한 맵핑된 MVC컨트롤러의 클래스에 속한 메소드 확인 • .andDo(MockMvcResultHandlers.print()); • 해당 요청과 응답에 대한 처리를 하는 부분 • MockMvcResultHandlers.print() • Spring Test MVC 응답 결과에 대한 모든 정보 출력 처리
  • 23. Spring Test MVC 구조 Perform => 요청 사전조건 Expect => 응답 관련 테스트 Do => 테스트시 직접 실행 Return => 테스트 결과 반환
  • 26. MockMvcResult Matchers 종류 Request Test Handler Test Status Test Header Test Model Test View Test Content Test ForwardUrl Test RedirectUrl Test Flash attribute Test Http Servlet 관련 Test Spring MVC 관련 Test JsonPath Test XPath Test Content Data Test
  • 27. • @Test • public void jsonPathTest() throws Exception { • mockMvc.perform(get("/test/") • .header("x-requested-with", "XMLHttpRequest") • .accept(MediaType.APPLICATION_JSON)) • .andExpect(handler().handlerType(ApiController.class)) • .andExpect(handler().methodName(”getJson")) • .andExpect(jsonPath("$.status").value("FAIL”)) • .andDo(MockMvcResultHandlers.print()); • } • } JSONPath Test 예제
  • 28. JSONPath 사용 하기위해는? • JSONPath의 사용방법 필요 • 참조 : http://goessner.net/articles/JsonPath/ • com.jayway.jsonpath 라이브러리 필요
  • 29. 추가적인 질문?! Servlet filter는 어떻게 테스트 하나요!? Spring security는 어떻게 테스트하나요? Spring Test MVC로 가능한가요? …
  • 30. @RunWith(SpringJUnit4ClassRunner.class) • @ContextConfiguration(classes=WebAppConfig.class) • @WebAppConfiguration • public class ControllerTest { • @Autowired • FilterChainProxy springSecurityFilterChain; • @Autowired • WebApplicationContext wac; • MockMvc mockMvc; • @Before • public void init() { • mockMvc = MockMvcBuilders.webAppContextSetup(wac) • .addFilters(springSecurityFilterChain).build(); • } Spring security 연동 예제
  • 31. Spring security 연동 시 참고 • 참조 소스 • 참조 : https://github.com/SpringSource/spring-test- mvc/blob/master/src/test/java/org/springframework/test/web/server/ samples/context/SpringSecurityTests.java • Spring security filter가 설정된 MockMvc객체를 사용시 모든 요청이 권한과 관계가 맺어지기 때문에 신경써서 Test작성이 필요
  • 32. … @Before • public void init() { • mockMvc = MockMvcBuilders.webAppContextSetup(wac) • .alwaysDo(MockMvcResultHandlers.print()) • // 모든 테스트 정보를 logging을 한다. • .alwaysExpect(status().isOk()) • // 모든 테스트는 http status 코드가 200 이어야한다. • .build(); • } 마지막 팁! • 공통적인 테스트 및 실행할 코드를 선언하는 방법 • 하단 예제 참조
  • 35. Request test 주요 메소드 요약 • Request attribute test ( Attribute 정보 확인 테스트 ) • .andExpect(request().attribute(.., ..)) • Session attribute test ( 세션 정보 확인 테스트 ) • .andExpect(request().sessionAttribute(.., ..)) • Async test ( Servlet 3.0, Spring 3.2 부터 지원 하는 비동기 요청 테스트 ) • .andExpect(request(). asyncStarted(.., ..)) • 비동기요청인가? • .andExpect(request(). asyncNotStarted(.., ..)) • 비동기요청이 아닌가? • .andExpect(request(). asyncResult(.., ..)) • 비동기 요청에 대한 응답 정보는?
  • 36. Handler test 주요 메소드 요약 • Handler type test ( 요청에 맵핑 되는 Handler 타입을 테스트 ) • .andExpect(handler().handlerType(..)) • Handler method name test ( 요청에 맵핑되는 Handler 메소드명 테스트 ) • .andExpect(handler().methodName(..)) • Handler method type test ( 요청에 맵핑되는 Handler 메소드타입 테스트) • .andExpect(handler().method(..))
  • 37. Model test 주요 메소드 요약 • Model attrbute test ( Model 속성 테스트 ) • .andExpect(model().attribute(..)) • .andExpect(model().attributeExists(..)) • .andExpect(model().attributeErrorCount(..)) • .andExpect(model().attributeHasErrors(..)) • .andExpect(model().attributeHasNoErrors(..)) • .andExpect(model().attributeHasFieldErrors(..)) • .andExpect(model().errorCount(..)) • .andExpect(model().hasErrors(..)) • .andExpect(model().hasNoErrors(..)) • 일단 일일이 설명 보다는 메소드명에 모든 설명이 담겨있다!! ( 귀찮아서 그런거 절대 아님! ) • 대략 해당 Model에 들어 이는 속성에 대한 값과 Validation체크로 통한 에러 여부 등을 체크한다.
  • 38. View test 주요 메소드 요약 • View name test ( 해당 요청에 선택된 View name 테스트 ) • .andExpect(view().name(..))
  • 39. Status, header test 주요 메소드 요약 • Status test ( 응답 상태에 대한 테스트 ) • .andExpect(status().is(..)) • 응답상태 코드에 대한 테스트 ( 200:정상 … ) • org.springframework.http HttpStatus 참조 • Error message test ( 에러메세지에 대한 테스트 ) • .andExpect(status().reason(..)) • 메세지 문자열 등 테스트 한다. • Header test ( 헤더 정보에 대한 테스트 ) • .andExpect(header().string(..)) • .andExpect(header().longValue(..))
  • 40. Content Test 주요 메소드 요약 • ContentType test( contentType 테스트 ) • .andExpect(content().contentType(..)) • .andExpect(content().contentTypeCompatibleWith(..)) • Encoding test ( content 문자열 인코딩 테스트 ) • .andExpect(content().encoding(..)) • Content test ( content 테스트 ) • .andExpect(content().string(..)) : content 문자열 비교 • .andExpect(content().bytes(..)) : content byte 비교 • .andExpect(content().xml(..)) : content xml dom 비교 • .andExpect(content().node(..)) : content xml dom node 비교 • .andExpect(content().source(..)) : content xml dom source 객체 비교
  • 41. Other Test 주요 메소드 요약 • Forwarded url test( 포워딩 URL 테스트 ) • .andExpect(forwardedUrl(..)) • Redirected url test ( 리다이렉트 URL 테스트 ) • .andExpect(redirectedUrl(..)) • Flash attribute test ( Spring 3.1 부터 지원 되는 Flash attribute값 테스트 ) • .andExpect(flash().attribute(..)) : attribute 테스트 • .andExpect(flash(). attributeExists(..)) : attribute 존재 여부 테스트 • .andExpect(flash(). attributeCount(..)) : attribute의 수 테스트
  • 42.

Hinweis der Redaktion

  1. 안녕하세요 이번 발표를 하게된 KSUG의 이수홍이라고합니다.이번에는 Spring Test MVC라는 주제로 발표해 보겠습니다.일단 많은 내용이 있습니다만 짧은 시간안에 발표하기 위해 많이 단축했습니다.추가적인 기능은 문서를 확인하시면 많은 기능을 확인 할 수 있습니다.
  2. 기존에는 웹서버에서 요청 응답 테스트 할 때에도 이런 방법도 사용했었습니다.
  3. 물론 다른 방법도 있습니다.
  4. Mock객체를 이용해서 직접 맵핑되는 Handler(보통 Controller라 불리는 )를 테스트 하기도 했습니다.
  5. Mock을 코드테스트의 문제는 Spring MVC에서 발생하게 됩니다.
  6. Spring MVC 3.0 이부 후터는 Request 객체를 직접 사용 추천 하지 않습니다.( 물론 가능은 합니다.)다양한 Handler를 작성할 수 있는 Spring @MVC에서는 Mock객체를 이용한 테스트는 어울리지 않습니다.
  7. 그렇습니다. 그냥 객체를 생성시켜 메소드 호출도 물론 가능합니다.
  8. 이렇게 말이죠.
  9. 문제는 과연 이 테스트의 결과가 만족스러운가요?
  10. 그 외 Spring 에서 지원하는
  11. Spring
  12. Spring TestMVC는 3.0 이후 사용한 @MVC 방식의 프레임워크가 기존 테스트방식으로 테스트 하기에는 많은 한계를 느끼고Spring 3.1부터 독립적이 프로젝트로 진행 해오다가 Spring3.2부터 Spring Test에 내장되었습니다.
  13. 기존 테스트 방식이 코드 기준으로 Unit 형태로 테스트를 했다고 하면
  14. Spring Test MVC는 MockMVC라는 Spring MVC 작동하는 가상 서버 역할을 하는 객체가 만들어지게 됩니다.그 객체에서 직접 URI,Parameter, 메소드, 헤더 등으로 HttpRequest형식로 요청하게됩니다.실제 Spring MVC 프레임워크가 서버에서 작동하는 방법과 동일하게 실행시켜 실제 서버와 결과값을 동일 하게 Response형태를 받아서 테스트하게 됩니다.
  15. 실제 Web Application Server를 띄우지 않고 Spring MVC 테스트가 가능합니다.
  16. 실제 Was에서 Spring MVC를 실행한것과 Spring Test MVC에서 한것과 요청에 대한 결과는 대부분은 동일
  17. Junit의 test를 위한 메소드는 직접 사용하지 않아도 내부적으로 사용해서 테스트를 진행합니다.
  18. 어디까지 테스트해봤니?
  19. 결과값이 Json이면 파싱을 하여 jsonPath로 테스트를 진행합니다.
  20. 권한인증 프레임워크에서는 Spring security를 많이 사용 하고 계실텐데요Spring Test MVC로 테스틑 하는 방법을 추가적으로 보여 드리겠습니다.
  21. 결과값이 Json이면 파싱을 하여 jsonPath로 테스트를 진행합니다.
  22. 해단 mockMvc를 사용하는 요청에서 공통적인 테스트 및 실행할 코드를 설정 가능한 방법이다.