Spock Unit Test
JUnit + Hamcrest + Mokioto 의 조합을 한 방에 해결하는 Spock 을 STS 환경에서 사용할 수 있도록 구성해 보자.
참고 자료:
맛보기: http://helloworld.naver.com/helloworld/568425 (강력 추천)
환경 설정: http://javacan.tistory.com/313
따라하기: https://code.google.com/p/spock/w/list
본 문서를 보기 전 맛보기를 꼭 읽어 보길 권장함
작성자: 초보람보
이메일: expert0226@gmail.com
목차
Groovy Eclipse Plugin 설치 (설치 안 된 경우만)
Project Conext Menu > Jspresso > Add Spock Nature
Project Context > Groovy > Add Groovy Libraries to classpath 클릭
HelloSpock.groovy Context Menu > Run As > Junit Test 실행
pom.xml 에 spock 및 groovy 관련 의존성 추가 (전체 pom.xml 은 하단에 첨부)
pom.xml 의 plugin 중 maven-compiler-plugin 에 groovy compiler 정보 추가
<plugin> 에서 에러 발생 시에 Groovy-Eclipse m2e 커넥터 추가
src/test/java에 HelloSpock.groovy Groovy Test Case 추가
Project Conext Menu > Jspresso > Add Spock Nature 추가
HelloSpock.groovy Context Menu > Run As > Junit Test 실행
problem 뷰의 groovy compiler version 문제가 보기 싫은 경우
1. 기반 환경
JAVA 8
STS 3.6.3.RELEASE - Eclipse Luna SR1 (4.4.1)
2. Groovy Eclipse Plugin 설치 (설치 안 된 경우만)
Groovy/Grails Tool Suites (GGTS) for Eclipse Luna (4.4) 3.63 RELEASE
3. Spock Plugin 설치 – Spock Plugin 2.10.0
4. 테스트용 java project 추가
참고: https://code.google.com/p/spock/wiki/GettingStarted
4.1 New > Java Project
Project name: HelloSpack
4.2 Project Conext Menu > Jspresso > Add Spock Nature
4.3 New > Groovy Test Class
Name: HelloSpock
오류가 발생하는 경우
Errors occurred during the build. Errors running builder 'Java Builder' on project 'HelloSpack'. org/codehaus/jdt/groovy/integration/LanguageSupportFactory |
Groovy Eclipse Plugin 설치 강화
위 그림에서 Lean more
위 그림에서 Install 클릭
요소 전체 선택 -> Confirm -> Finish
오류가 발생하는 경우 2 – Groovy Test Case 생성 시 Groovy Project 가 아니라고 반항하는 경우
2015년 1월 5일 업데이트된 플러그인으로 인한 오류로 추정
증상:
프로젝트의 Java Build Path에서 GROOVY_SUPPORT(unbound)로 나오게 됨
해결책:
프로젝트 컨텍스트 메뉴 configure > Convert to Groovy Project 선택 후 진행하면 됨
Groovy 프로젝트도 JAVA 프로젝트의 확장이기에 java 기능과 빌드에 전혀 문제가 없음
configure > Convert to Groovy Project 후의 Build Path 정보
4.4 소스 코드 입력
import spock.lang.*
class HelloSpock extends spock.lang.Specification { def "length of Spock's and his friends' names"() { expect: name.size() == length
where: name | length "Spock" | 5 "Kirk" | 4 "Scotty" | 6 } } |
4.5 Build Path 에 spock library 추가
spock-core-0.7-groovy-2.0.jar
다운로드:
http://repo1.maven.org/maven2/org/spockframework/spock-core/0.7-groovy-2.0/spock-core-0.7-groovy-2.0.jar
또는
https://code.google.com/p/spock/wiki/GettingStarted 에서 링크 클릭
4.6 Project Context > Groovy > Add Groovy Libraries to classpath 클릭
4.7 HelloSpock.groovy Context Menu > Run As > Junit Test 실행
1. Spring MVC Project 생성
New > Spring Project
Project name: SpringMVCSpock
2. pom.xml 에 spock
및 groovy 관련 의존성 추가
(전체 pom.xml 은 하단에 첨부)
<dependency> <groupId>org.spockframework</groupId> <artifactId>spock-core</artifactId> <version>0.7-groovy-2.0</version> <!-- 0.7-groovy-1.8 --> <scope>test</scope> </dependency>
<dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-all</artifactId> <version>2.3.7</version> <scope>test</scope> </dependency>
<dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-backports-compat23</artifactId> <version>2.3.5</version> </dependency>
<dependency> <groupId>org.codehaus.groovy.maven.runtime</groupId> <artifactId>gmaven-runtime-default</artifactId> <version>1.0-rc-3</version> <exclusions> <exclusion> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-all</artifactId> </exclusion> </exclusions> </dependency>
<!-- <dependency> <groupId>bsf</groupId> <artifactId>bsf</artifactId> <version>2.4.0</version> </dependency>
<dependency> <groupId>org.apache.ivy</groupId> <artifactId>ivy</artifactId> <version>2.4.0</version> </dependency> --> |
3. pom.xml 의 plugin 중 maven-compiler-plugin 에 groovy compiler 정보 추가
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.0</version> <configuration> <source>1.8</source> <target>1.8</target> <compilerArgument>-Xlint:all</compilerArgument> <showWarnings>true</showWarnings> <showDeprecation>true</showDeprecation> <compilerId>groovy-eclipse-compiler</compilerId> </configuration> <dependencies> <dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-eclipse-compiler</artifactId> <version>2.8.0-01</version> </dependency> <dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-eclipse-batch</artifactId> <!-- Groovy version --> <version>2.1.8-01</version> </dependency> </dependencies> </plugin> |
4. <plugin> 에서 에러 발생 시에 Groovy-Eclipse m2e 커넥터 추가
위 3개의 커넥터를 모두 선택해서 설치
5. src/test/java에 HelloSpock.groovy Groovy Test Case 추가
6. Project Conext Menu > Jspresso > Add Spock Nature 추가
7. HelloSpock.groovy Context Menu > Run As > Junit Test 실행
8. problem 뷰의 groovy compiler version 문제가 보기 싫은 경우
Window > Preference > Groovy > Compiler 버전은 2.0.7로 변경
참고. pom.xml 전체 소스
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.haven</groupId> <artifactId>spock</artifactId> <name>SpringMVCSpock</name> <packaging>war</packaging> <version>1.0.0-BUILD-SNAPSHOT</version> <properties> <java-version>1.8</java-version> <org.springframework-version>4.1.3.RELEASE</org.springframework-version> <org.aspectj-version>1.6.10</org.aspectj-version> <org.slf4j-version>1.6.6</org.slf4j-version> </properties> <dependencies> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework-version}</version> <exclusions> <!-- Exclude Commons Logging in favor of SLF4j --> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${org.springframework-version}</version> </dependency>
<!-- AspectJ --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>${org.aspectj-version}</version> </dependency>
<!-- Logging --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${org.slf4j-version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>${org.slf4j-version}</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${org.slf4j-version}</version> <scope>runtime</scope> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.15</version> <exclusions> <exclusion> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> </exclusion> <exclusion> <groupId>javax.jms</groupId> <artifactId>jms</artifactId> </exclusion> <exclusion> <groupId>com.sun.jdmk</groupId> <artifactId>jmxtools</artifactId> </exclusion> <exclusion> <groupId>com.sun.jmx</groupId> <artifactId>jmxri</artifactId> </exclusion> </exclusions> <scope>runtime</scope> </dependency>
<!-- @Inject --> <dependency> <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> <version>1</version> </dependency>
<!-- Servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency>
<!-- Test --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency>
<dependency> <groupId>org.spockframework</groupId> <artifactId>spock-core</artifactId> <version>0.7-groovy-2.0</version> <!-- 0.7-groovy-1.8 --> <scope>test</scope> </dependency>
<dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-all</artifactId> <version>2.3.7</version> <scope>test</scope> </dependency>
<dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-backports-compat23</artifactId> <version>2.3.5</version> </dependency>
<dependency> <groupId>org.codehaus.groovy.maven.runtime</groupId> <artifactId>gmaven-runtime-default</artifactId> <version>1.0-rc-3</version> <exclusions> <exclusion> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-all</artifactId> </exclusion> </exclusions> </dependency>
<dependency> <groupId>bsf</groupId> <artifactId>bsf</artifactId> <version>2.4.0</version> <scope>test</scope> </dependency>
<dependency> <groupId>org.apache.ivy</groupId> <artifactId>ivy</artifactId> <version>2.4.0</version> <scope>test</scope> </dependency>
</dependencies> <build> <plugins> <plugin> <artifactId>maven-eclipse-plugin</artifactId> <version>2.9</version> <configuration> <additionalProjectnatures> <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature> </additionalProjectnatures> <additionalBuildcommands> <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand> </additionalBuildcommands> <downloadSources>true</downloadSources> <downloadJavadocs>true</downloadJavadocs> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.0</version> <configuration> <source>1.8</source> <target>1.8</target> <compilerArgument>-Xlint:all</compilerArgument> <showWarnings>true</showWarnings> <showDeprecation>true</showDeprecation> <compilerId>groovy-eclipse-compiler</compilerId> </configuration> <dependencies> <dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-eclipse-compiler</artifactId> <version>2.8.0-01</version> </dependency> <dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-eclipse-batch</artifactId> <!-- Groovy version --> <version>2.1.8-01</version> </dependency> </dependencies> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.2.1</version> <configuration> <mainClass>org.test.int1.Main</mainClass> </configuration> </plugin> </plugins> </build> </project> |
일반 Maven 프로젝트의 경우 https://code.google.com/p/spock/wiki/HelloSpockPom 참고
해당 위키에는 Ant, Gradle 설정도 제공해 준다.
'강좌 > TDD & Test' 카테고리의 다른 글
Mockito 002 reset (0) | 2013.04.29 |
---|---|
Mockito 001 (0) | 2013.04.29 |
Spring MVC Test 001 (0) | 2013.04.26 |