Monday, 28 September 2009

Running Groovy using Maven2

Share
Last week I wrote a post about GroovyWS (GroovyWS terrifies me! - DZone Big Link on 09/27 - thanks everyone!).

Using Eclipse Galileo I created Maven2 project and added Groovy nature. Eclipse automatically took care of the Groovy runtime so I was able to run Groovy classes just as they were standard Java classes.

Today I will show you how to run Groovy classes (here Groovy JUnit tests) using Maven2.

Setting up the project

Please do refer to my previous post: GroovyWS terrifies me!. Complete working download can be find at the end of this post.

Running tests using Maven2

When you execute standard Maven2 test command:
mvn test
Maven2 doesn't pick-up and execute Groovy test:
C:\Studies\PhD\workspace\cxf-and-groovy>mvn test
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Simple CXF project using spring configuration
[INFO]    task-segment: [test]
...
[INFO] [compiler:testCompile]
[INFO] No sources to compile
[INFO] [surefire:test]
[INFO] No tests to run.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7 seconds
[INFO] Finished at: Fri Sep 25 14:46:01 CEST 2009
[INFO] Final Memory: 17M/30M
[INFO] ------------------------------------------------------------------------
We have to make Maven2 aware of Groovy in order to pick-up and execute Groovy classes.

First, I added gmaven-runtime-default into Maven's dependencies list:
<dependency>
<groupId>org.codehaus.groovy.maven.runtime</groupId>
<artifactId>gmaven-runtime-default</artifactId>
<version>1.0-rc-3</version>
</dependency>
Then, I appended gmaven-plugin to following execution goals:
<plugins>
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>generateStubs</goal>
<goal>compile</goal>
<goal>generateTestStubs</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
Running Groovy JUnit tests

I re-ran the test and was surprised to see the following error:
C:\Studies\PhD\workspace\cxf-and-groovy>mvn test
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Simple CXF project using spring configuration
[INFO]    task-segment: [test]
[INFO] ------------------------------------------------------------------------
[INFO] [groovy:generateStubs {execution: default}]
[INFO]  No sources found for Java stub generation
...
[INFO] [groovy:compile {execution: default}]
[INFO]  No sources found to compile
[INFO] [groovy:generateTestStubs {execution: default}]
[INFO]  Generated 1 Java stub
...
[INFO] [compiler:testCompile]
[INFO] Compiling 1 source file to C:\Studies\PhD\workspace\cxf-and-groovy\target\test-classes
[INFO] [groovy:testCompile {execution: default}]
[INFO]  Compiled 1 Groovy class
[INFO] [surefire:test]
[INFO] Surefire report directory: C:\Studies\PhD\workspace\cxf-and-groovy\target\surefire-reports

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running org.xh.studies.ws.cxfandgroovy.HelloWorldTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.047 sec <<
Fixing the test failure I opened C:\Studies\PhD\workspace\cxf-and-groovy\target\surefire-reports\org.xh.studies.ws.cxfandgroovy.HelloWorldTest.txt file and saw:
-------------------------------------------------------------------------------
Test set: org.xh.studies.ws.cxfandgroovy.HelloWorldTest
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.063 sec <<<>(TestClass.java:36)
at org.junit.runners.ParentRunner.(ParentRunner.java:54)
...
I did some googling and find out this:
http://jira.codehaus.org/browse/GMAVEN-36
The bug is still open, but in comments there is a workaround which basically boils down to adding the following property into pom.xml file:
<properties>
<gmaven.runtime>1.5</gmaven.runtime>
</properties>
This time, when I re-ran my test I saw lots of Apache CXF debug messages and finally:
...
WARNING:  Using SOAP version: 1.1
2009-09-25 14:56:51 org.apache.cxf.interceptor.LoggingOutInterceptor$LoggingCallback onClose
INFO: Outbound Message
---------------------------
ID: 2
Address: http://localhost:6080/cxf-and-groovy/HelloWorld
Encoding: UTF-8
Content-Type: text/xml
Headers: {SOAPAction=[""], Accept=[*/*]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns1:sayHiToPerson xmlns:ns1="http://cxfandgroovy.ws.studies.xh.or
g/"><arg0 xmlns:ns2="http://cxfandgroovy.ws.studies.xh.org/"><firstName>Lukasz</firstName></arg0></ns1:sayHiToPerson></soap:Body></soap:Envelope>
--------------------------------------
2009-09-25 14:56:51 org.apache.cxf.interceptor.LoggingInInterceptor logging
INFO: Inbound Message
----------------------------
ID: 2
Encoding: UTF-8
Content-Type: text/xml;charset=UTF-8
Headers: {content-type=[text/xml;charset=UTF-8], Date=[Fri, 25 Sep 2009 12:56:51 GMT], Content-Length=[257], Server=[Apache-Coyote/1.1]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHiToPersonResponse xmlns:ns2="http://cxfandgroovy.ws.studi
es.xh.org/"><return><text>Hello Lukasz</text></return></ns2:sayHiToPersonResponse></soap:Body></soap:Envelope>
--------------------------------------
testSayHiToPerson ==> Hello Lukasz
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 35.953 sec

Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 50 seconds
[INFO] Finished at: Fri Sep 25 14:56:51 CEST 2009
[INFO] Final Memory: 21M/38M
[INFO] ------------------------------------------------------------------------
Summary and complete working example download That's all for today, now you know how to execute Groovy tests (and not only) using Maven2. Complete working example is available here: GroovyWS-Apache-CXF-Maven2.zip. Cheers, Łukasz

5 comments:

Steven said...

seems the download has the test set to be ignored.
Also, need to add repository to pom.xml: http://people.apache.org/repo/m2-incubating-repository

Łukasz said...

Hi Steven,

Yes, the test is set to be ignored, because when you build the project using:

mvn package

without having Web Service deployed test would simply fail, that is why the test is to be ignored in the download.

After project is deployed, you can run the test without any problems:

mvn package
mvn test

The second thing depends on your Maven2 configuration. I didn't have to add anything else to the pom.xml file, but thanks for the hint, it may be useful for someone else having the same problem you had.

Cheers,
Łukasz

Steven said...

hi lukasz,

i was able to pull off the testing by separating out the hello test into an integration test and have surefire kick it off *after* starting an embedded jetty (which gets shutdown after integration tests).

can be tricky to setup for the first time, but it pays off.

Łukasz said...

Steven,

Thanks for the tip.

When I find some time next week I will write about integration testing.

I will re-use the Groovy CXF app.

Cheers,
Łukasz

viagra online said...

sts run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.063 sec <<<>(TestClass.java:36) at org.junit.runners.ParentRunner.(ParentRunner.java:54) ...I did some googling and find out this:
http://jira.codehaus.org/browsebut..
It is difficult to do , In my personal opinion I can do it because I have not studied about it in a good way.