Spring MVC 테스트 자동화 및 커버리지 분석
Spring MVC 테스트 자동화 및 Jacoco를 이용한 커버리지 분석 방법에 대해 소개한다.
Spring MVC 테스트 자동화를 위해 MockMvc를 이용하여 Controller를 호출하고, 그 결과를 검증하는 테스트 자동화 방법이다.
프로젝트 빌드 시 테스트 케이스가 수행되고, 수행 로직에 따라 커버리지 결과가 자동으로 생성한다.
먼저 pom.xml에 Dependency를 설정한다.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test-mvc</artifactId>
<version>1.0.0.M1</version>
<scope>test</scope>
</dependency>
pom.xml 파일에 Dependency를 추가한 후 Maven build시 자동으로 테스트 케이스와 커버리지를 수행하기 위해 plugin을 다음과 같이 설정한다.

설정이 완료된 후 테스트 케이스를 작성한다.
테스트 케이스를 작성하기 전에 공통적으로 설정이 필요한 클래스를 작성하여 모든 테스트 클래스에서 상속하여 사용하도록 한다.
다음은 테스트를 위한 공통 클래스이다.

다음은 테스트를 위한 클래스를 생성한다.

모든 작업이 완료된 후 Eclipse에서 Maven install을 수행한다.
테스트 수행이 완료된 후 다음과 같은 결과를 볼수 있다.
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
테스트 케이스에 대한 결과 파일은 workspace/프로젝트명/target/surefile-reports 디렉토리에 생성된다.

테스트에 대한 커버리지는 workspace/프로젝트명/target/site/jacoco 디렉토리에 생성된다.

index.html을 클리하면 브라우저에 다음과 같은 화면을 통해 테스트 커버리지를 확인할 수 있다.
