码迷,mamicode.com
首页 > 其他好文 > 详细

Groovy Spock环境的安装

时间:2016-12-18 21:11:51      阅读:585      评论:0      收藏:0      [点我收藏+]

标签:下载地址   例子   解决   oal   打开   eclips   成功   unit   命令行   

听说spock是一个加强版的Junit,今天特地安装了,再把过程给大家分享一下。
首先说明,我的Java项目是用maven管理的.
我用的Eclipse是Kelper,开普勒。
要使用Spock之前,首先要先把Groovy的环境配好。
 
上网搜了一下,找到了Groovy Eclipse插件的下载地址:
 
 
刚好这个安装包就是用于开普勒的。
更高级版本的Eclipse好像只能在线安装,目前还没有离线安装包可以下载。
 
有这个安装包就可以把Groovy的Eclipse环境配好,不过我也发现过有一些Eclipse会装不起来,上网下了一个新的Eclipse就能装起,可能是跟Eclipse的某些插件冲突有关。
 
打开Eclipse的配置,会很惊异地发现,多了一个Groovy项。选中Compiler一项,发现支持的编译器的版本从1.8.6到2.2.2,再高的就不能支持了,估计是跟Eclipse的Groovy插件有关的,如果要往上支持,可能要用更新版本的Eclipse了。
 
毫不犹豫地我选取了最新的版本2.2.2.
在Pom文件里面添加了以下的依赖,
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.2.2</version>
</dependency>
 
下一步安装Spock,从Spock的Wiki找到了版本对应的关系。
| Spock version | Groovy version | JUnit version | Grails version | Spring version |
| 0.5-groovy-1.6 | 1.6.1-1.6.x | 4.7-4.x | 1.2.0-1.2.x | 2.5.0-3.x |
| 0.5-groovy-1.7 | 1.7.0-1.7.x | 4.7-4.x | 1.3.0-1.3.x | 2.5.0-3.x | 
| 0.6-groovy-1.7 | 1.7.0-1.7.x | 4.7-4.x | 1.3.0-1.3.x | 2.5.0-3.x | 
| 0.6-groovy-1.8 | 1.8.1-1.8.x | 4.7-4.x | 2.0-2.x | 2.5.0-3.x | 
| 0.7-groovy-1.8 | 1.8.1-1.8.x | 4.7-4.x | 2.0-2.x | 2.5.0-3.x | 
| 0.7-groovy-2.0 | 2.0.0 -2.x.x | 4.7-4.x | 2.2-2.x | 2.5.0-3.x | 
| 1.0-groovy-2.0 | 2.0.0 -2.2.x | 4.7-4.x | 2.2-2.x | 2.5.0-4.x | 
| 1.0-groovy-2.3 | 2.3.0 -2.3.x | 4.7-4.x | 2.2-2.x | 2.5.0-4.x | 
| 1.0-groovy-2.4 | 2.4.0 -2.x.x | 4.7-4.x | 2.2-2.x | 2.5.0-4.x |
发现要支持2.2.2,只能使用1.0-groovy-2.0的版本。
 
所以我又毫不犹豫地添加了以下的依赖。
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>1.0-groovy-2.0</version>
</dependency>
 
好,立刻来一个Hello World。
package com.test.groovytest

import spock.lang.Specification;

class AddTest extends Specification{
Add add = new Add();
int result;
def "test add 1 and 2" () {
given: "there are two number 1 and 2"
add.a = 1;
add.b = 2;
when: "add them"
result = add.add();
then: "result should be 3"
result == 4;
}
}
这是一个最简单的对一个加法器的Spock测试。但是写好以后,并不能在Eclipse里面运行测试。
发现Junit的版本不对,立即改用最新的。
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
OK, 测试可以在Eclipse里面跑起来了。
但是持续集成是在命令行里面运行的,到命令行里面mvn clean compile一下,发现Groovy都没有编译。
上网找了一下,必须要在pom文件里面添加plugin。
好吧,把Plugin的配置加上
<plugins>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<goals>
<goal>addSources</goal>
<goal>addTestSources</goal>
<goal>generateStubs</goal>
<goal>compile</goal>
<goal>testGenerateStubs</goal>
<goal>testCompile</goal>
<goal>removeStubs</goal>
<goal>removeTestStubs</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
发现项目里面的groovy文件还是没能编译出来,上网再找,发现网上的例子都要把Groovy文件放进新开的编译路径下面,这个编译路径的名字是
src/main/groovy
src/test/groovy
 
把这两个路径创建好,把groovy文件挪过来,再mvn clean test。OK,成功了。
 
但是这个时候在Eclipse里面发现以下错误提示,
 
Plugin execution not covered by lifecycle configuration
 
在stackoverflow那里找到解决方法。
在pom文件里面添加
<pluginManagement>
<plugins>
<!--This plugin‘s configuration is used to store Eclipse m2e settings
only. It has no influence on the Maven build itself. -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>
org.codehaus.gmavenplus
</groupId>
<artifactId>
gmavenplus-plugin
</artifactId>
<versionRange>[1.5,)</versionRange>
<goals>
<goal>addSources</goal>
<goal>addTestSources</goal>
<goal>generateStubs</goal>
<goal>compile</goal>
<goal>testGenerateStubs</goal>
<goal>testCompile</goal>
<goal>removeStubs</goal>
<goal>removeTestStubs</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
那么就找不到其他的问题,我们可以开始愉快的编写Spock测试用例了。
 

Groovy Spock环境的安装

标签:下载地址   例子   解决   oal   打开   eclips   成功   unit   命令行   

原文地址:http://www.cnblogs.com/rocky1018/p/groovy-spock-eclipse-setup.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!