如何在运行集成测试时将Grails作为Web应用程序启动

时间:2010-09-15 01:21:42

标签: java grails groovy integration-testing

我正在尝试使用发出http请求的集成测试来测试我的grails应用。实际上它是硒/碲测试,但没关系。但正如我所知,当我运行grails测试时,它不会启动Web容器,同时我在博客文章中看到很多例子,人们使用selenium和其他需要http访问的测试工具来测试grails应用程序。

我创建了一个空的grails应用程序:

mvn org.apache.maven.plugins:maven-archetype-plugin:2.0-alpha-4:generate -DarchetypeGroupId=org.grails -DarchetypeArtifactId=grails-maven-archetype -DarchetypeVersion=1.3.4 -DgroupId=example -DartifactId=testportapp

cd testportapp/

mvn initialize

mvn grails:test-app

Tests PASSED - view reports in target/test-reports
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL

没关系。

现在添加一个尝试连接到grails test/integration/ListenPortTest.groovy的集成测试:

class ListenPortTest extends GroovyTestCase {
        void testPort(){
                def s = new Socket("localhost", 8080);
                s.close()
        }        
}

再次运行测试。现在我们收到以下异常:

Connection refused
   java.net.ConnectException: Connection refused
   ....

我也使用浏览器,wget和netstat检查了它,看起来像grails没有启动或没有打开任何端口。

问题: 如何在执行集成测试时配置grails来打开端口?

2 个答案:

答案 0 :(得分:12)

Grails默认支持两种类型的测试,单元和集成,以及使用插件的功能测试。单元和集成测试非常相似,并且实际上都是单元测试,除了集成测试具有初始化的Spring应用程序上下文,Hibernate配置,内存数据库等。但是没有正在运行的Web服务器 - 您需要进行功能测试。

功能测试有几种选择,最常用的是WebTest:http://grails.org/plugin/webtest,功能测试插件:http://grails.org/plugin/functional-test和Selenium RC插件:http://grails.org/plugin/selenium-rc

最新的是Geb:http://grails.org/plugin/geb如果您正在寻找Selenium支持,那将是您最好的选择。手册位于http://geb.codehaus.org/,最近有一篇博客文章写在这里:http://blog.springsource.com/2010/08/28/the-future-of-functional-web-testing/

答案 1 :(得分:3)

我认为你正在寻找这个插件

http://www.grails.org/plugin/functional-test