Mai 16 2011

How to create a Grails Web Service with a Selenium Test

Published by at 8:28 PM under Grails,intellij idea



Table of contents



Introduction

For a Customer Project i had to write a SOAP Web Service (for Interaction with an iPhone App) and for Test Driven Development i needed an easy way to create some Tests. First, i wrote a JUnit Test. Ok it worked, but the problem is that it is not usable with Continuous Integration, because it can only be tested against a running Grails-App. So i decided to use the good old Selenium-Framework.

The Selenium-Framework starts the Grails-App (with the Server) and runs the Tests against it! Now i want to show you the (simple!) solution.

I am using Grails 1.3.7 .


Create the Application

First, you have to add the Plugins “cxf” and “selenium-rc” to your project (in this example the project is named as “GrailsWebServiceWithSeleniumTest“. Here is my configuration-file:


application.properties

app.grails.version=1.3.7
app.name=GrailsWebServiceWithSeleniumTest
app.servlet.version=2.4
app.version=0.1
plugins.cxf=0.7.0
plugins.hibernate=1.3.7
plugins.selenium-rc=1.0.2
plugins.tomcat=1.3.7

Add this line to your “BuildConfig.groovy” to resolve the correct Maven dependency for the “Apache Httpclient commons“.


BuildConfig.groovy

runtime("commons-httpclient:commons-httpclient:3.1")

Create the Service “eu.jdevelop.blog.MyTestService” and insert this code:


MyTestService.groovy

package eu.jdevelop.blog

import javax.jws.WebParam
import javax.jws.WebResult
import javax.jws.WebService
import javax.jws.WebMethod

/**
 * Creates a Web Service with JAX-WS Annotations
 * URL: http://localhost:8080/GrailsWebServiceWithSeleniumTest/services/myTest?wsdl
 *
 * @author Siegfried Bolz
 */
@WebService(name = "MyTestWebService", targetNamespace = "eu.jdevelop.blog", serviceName = "MyTestWebServiceName")
class MyTestService {

  // Using Plugin cxf
  static expose = ['cxfjax']

  /**
   * Web Service Operation
   *
   * @param firstName String
   * @param lastName String
   * @return message String
   */
  @WebResult(name = "loginResult")
  @WebMethod(operationName = "login")
  String login(@WebParam(name = "firstname") String firstName, @WebParam(name = "lastname") String lastName) {
    return "Hello " + firstName + " " + lastName
  }
}

This is the Web Service, using JAX-WS-Annotations for the Contract Last approach. As you can see, i declare everything with the Annotations.

Now start the Grails Application, you can browse to it at:


http://localhost:8080/GrailsWebServiceWithSeleniumTest/services/myTest?wsdl


Created WSDL

Created WSDL


You can use Tools like “soapui” to create sample Requests for testing.


Create the Selenium Test

The next step is to create the Selenium Test. In Grails there is now the new Directory “/test/selenium“. Create the file “eu.jdevelop.blog.MyTestServiceSeleniumTests.groovy” and insert this:


MyTestServiceSeleniumTests.groovy

package eu.jdevelop.blog

import grails.plugins.selenium.SeleniumAware
import org.apache.commons.httpclient.HttpClient
import org.apache.commons.httpclient.methods.PostMethod
import org.apache.commons.httpclient.methods.StringRequestEntity

/**
 * Selenium-Test for the Web Service, uses Selenium for starting the Grails Server.
 *
 * @author Siegfried Bolz
 */
@Mixin(SeleniumAware)
class MyTestServiceSeleniumTests extends GroovyTestCase {

  void testLoginMethod() {
    selenium.open "http://localhost:8080/GrailsWebServiceWithSeleniumTest/services/myTest?wsdl"

    def url = "http://localhost:8080/GrailsWebServiceWithSeleniumTest/services/myTest?wsdl"

    def payload = """
            <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:eu="eu.jdevelop.blog">
              <soapenv:Header/>
                <soapenv:Body>
                  <eu:login>
                    <firstname>Siegfried</firstname>
                    <lastname>Bolz</lastname>
                  </eu:login>
                </soapenv:Body>
            </soapenv:Envelope>"""

    def method = new PostMethod(url)
    def client = new HttpClient()

    payload = payload.trim()
    method.addRequestHeader("Content-Type", "text/xml")
    method.addRequestHeader("Accept", "text/xml,application/xml;q=0.9")
    method.setRequestEntity(new StringRequestEntity(payload))
    def statusCode = client.executeMethod(method)
    println "STATUS CODE : ${statusCode}"

    assertEquals "Status Code 200 expected", 200, statusCode

    def resultsString = method.getResponseBodyAsString()
    println resultsString

    assertTrue "Result did not contain the expected result message", resultsString.contains("<loginResult>Hello Siegfried Bolz</loginResult>")

    method.releaseConnection()
  }

}

To start the test, execute this command:

test-app :selenium eu.jdevelop.blog.MyTestServiceSeleniumTests.testLoginMethod -echoOut

As VM parameters i recommend:

-Xmx1024m -Xms1024m -XX:NewSize=512m -XX:MaxNewSize=512m -XX:MaxPermSize=256m


Selenium Test in IDEA

Selenium Test in IDEA


Download

A working example for Grails 1.3.7 and IntelliJ IDEA 10 is available here (click)


Conclusion

As you can see, it is very simple to create a Web Service with a Selenium Test. If you have any comments, drop a line below.

Technorati Tags: , , , , , , , , , , , , ,

No responses yet

Comments are closed.