I wasn't mistaken.
Groovy has GroovyWS module which terrifies me... in an extremely positive way!
Want to see why?
Prerequisites
Eclipse 3.4 with Maven2 and Groovy plugins.
Setting up the project
Create a simple Maven2 project, as an archetype select:
groupdId: org.apache.cxf.archetype
artifactid: cxf-jaxws-javafirst
In my case, under
src/main/java, Maven2 created org.xh.studies.ws.cxfandgroovy package with a simple HelloWorld Web Service.I modified it a little bit (added
sayHiToPerson() method):@WebService
public interface HelloWorld {
String sayHi(String text);
Greetings sayHiToPerson(Person person);
}
@WebService(endpointInterface = "org.xh.studies.ws.cxfandgroovy.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
public String sayHi(String text) {
return "Hello " + text;
}
public Greetings sayHiToPerson(Person person) {
Greetings greetings = new Greetings();
greetings.setText("Hello " + person.getFirstName());
return greetings;
}
}
public class Person {
private String firstName;
private String lastName;
/* getter and setters */
}
public class Greetings {
private String text;
/* getter and setters */
}Then I built and deployed my project.Creating Groovy JUnit test
First, I added following dependencies to my
pom.xml file:<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.5</version> <scope>test</scope> </dependency> <dependency> <groupId>org.codehaus.groovy.modules</groupId> <artifactId>groovyws</artifactId> <version>0.5.0</version> <scope>test</scope> <exclusions> <exclusion> <artifactId>abdera-extensions-main</artifactId> <groupId>org.apache.abdera</groupId> </exclusion> <exclusion> <artifactId>abdera-i18n</artifactId> <groupId>org.apache.abdera</groupId> </exclusion> </exclusions> </dependency>(Maven2 complained about not being able to download abdera-extensions-main and abdera-i18n so I added two exclusions for them)
Then, I added Groovy nature to my project.
Finally, I created
src/test/groovy folder and org.xh.studies.ws.cxfandgroovy.HelloWorldTest Groovy class.I've already investigated GroovyWS so I already knew that it used Apache CXF underneath, that is why I automatically added Apache CXF logging interceptors.
Stub of my test looked like that:
public class HelloWorldTest {
def proxy
@Before
public void setUp() {
proxy = new WSClient('http://localhost:6080/cxf-and-groovy/HelloWorld?wsdl', this.class.classLoader)
// magical method
proxy.initialize()
// Apache CXF logging interceptors
proxy.client.getInInterceptors().add(new LoggingInInterceptor())
proxy.client.getOutInterceptors().add(new LoggingOutInterceptor())
}
}First test: sayHi()Using GroovyWS you just use the proxy object as if it was an instance of a Web Service, if you want to invoke a method called
sayHi() you just do it (and Groovy takes care of the rest):@Test
public void testSayHi() {
def result = proxy.sayHi('Lukasz')
def expected = 'Hello Lukasz'
assertEquals expected, result
println 'testSayHi ==> ' + result
}I thought it was nice, but would it handle complex types?Second test: sayHiToPerson()
Yes, of course it would! My test
sayHiToPerson() invocation looked like this:@Test
public void testSayHiToPerson() {
def person = new Person()
person.setFirstName('Lukasz')
def result = proxy.sayHiToPerson(person)
def expected = 'Hello Lukasz'
assertEquals expected, result.text
println 'testSayHiToPerson ==> ' + result.text
}SummaryIt's terrifying, it really is... I think that from now on I will be testing my Web Services using only GroovyWS!
Take care,
Łukasz

8 comments:
Is this grails plugin simply a groovified version of CXF? Doesn't that mean that you can do the same thing with plain CXF and Java
Hi sduskis,
You may say so, but GroovyWS comes with all Groovy goodies.
For example you can invoke Web Service just by writing method's signature and its parameters. During runtime GroovyWS constructs proper SOAP message and invokes remote Web Service.
This is pretty handy a simple WS test might be written in 3 lines of code.
Cheers,
Łukasz
@Łukasz: again, don't those specific goodies come from CXF as opposed the the grails packaging?
@sduskis, what grails plugin? This is groovy that he is using. The Groovy proxy figures out the operations using Groovy's Meta Programming I believe.
CXF was used to simply create the web service and the generation of the WSDL.
He's only using Groovy to actually test the CXF generated Web Service.
You are a lucky guy to have time for all this research. Viva Poland academics system for providing you this opportunity!
Hi there,
@Maxim Gubin: sduskis is right, because WSClient is using Apache CXF DynamicClient under the hood of Groovy Meta Programming :)
@Anonymous: it's not quite that simple, I work in IT industry full time so that I can earn decent money, I do my PhD and work with my students before or after my work. In total I work ~60 hours per week.
Cheers,
Łukasz
men I used this system for a time, the true is a very complicate to use and have a little bit of flexiblity in certains options.
great site
Post a Comment