Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

WebTest with Groovy, Maven and Eclipse

DZone's Guide to

WebTest with Groovy, Maven and Eclipse

· Java Zone
Free Resource

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

WebTest is in fact “only” a set of Ant tasks therefore it’s not a surprise that WebTest tests are traditionally written in XML. Grails WebTest plugin has shown that Groovy can be a very pleasant alternative thanks to the AntBuilder. Even if you don’t use Grails, you can write WebTest tests in Groovy using the support provided by WebTest’s distribution but if you plan to write all your tests in Groovy, this is surely not optimal. This post shows step by step a way to write WebTest tests that is near to a traditional setup for unit tests using Groovy and Maven.

Note: I’m not a Maven expert. The proposed code works but it can perhaps be improved.

  1. Create a new Maven project
  2. Edit the generated pom.xml
    1. Add WebTest as dependency:
          <dependencies>  
      ...
      <dependency>
      <groupId>com.canoo.webtest</groupId>
      <artifactId>webtest</artifactId>
      <version>3.1-SNAPSHOT</version>
      </dependency>
      </dependencies>
    2. Add reference to WebTest’s Maven snapshot repository

      Note that this is needed only if you want to use the latest WebTest snapshot (which is always the best one). You don’t need it if you use a version that is available on the central repository. As of Apr. 30, WebTest 3.0 has not been uploaded to the central repository. The upload request is available here: MAVENUPLOAD-2439.
    3. Configure the GMaven plugin
          <build>  
      <plugins>
      <plugin>
      <groupId>org.codehaus.groovy.maven
      </groupId>
      <artifactId>gmaven-plugin</artifactId>
      <version>1.0-rc-5</version>
      <executions>
      <execution>
      <goals>
      <goal>compile</goal>
      <goal>testCompile</goal>
      </goals>
      </execution>
      </executions>
      </plugin>
      <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>build-helper-maven-plugin</artifactId>
      <executions>
      <execution>
      <id>add-test-source</id>
      <phase>generate-sources</phase>
      <goals>
      <goal>add-test-source</goal>
      </goals>
      <configuration>
      <sources>
      <source>src/test/groovy</source>
      </sources>
      </configuration>
      </execution>
      </executions>
      </plugin>
      </plugins>
      </build>

  3. Create the Eclipse project
    mvn -Declipse.downloadSources=true eclipse:eclipse  

    I guess that something similar exists for other IDEs.

  4. Import the project in your workspace
  5. Add Groovy support in the IDE
    The Groovy support in Eclipse is not as good as in other IDEs but it is better than nothing.
  6. Write your first WebTest in Groovy src/test/groovy/my/domain/FirstWebTest.groovy
  7. Run the test
    • As normal unit test from the IDE
      As it is a normal unit test and you can use the IDE support for that. In Eclipse this can be done with right mouse click / Run As... / Junit Test or with the shortcut Alt + Shift + X Z
    • Or from the command line
  8. Enjoy the results!
    Running a WebTest this way produces “normal” JUnit reports as well as the traditional reports of WebTest that contain precise information to quickly find the cause of failed tests.
  9. (optional) Run headless
    Per default WebTest shows a console that informs about the running tests and opens the reports in your browser once the tests are finished. This is often useful on a workstation but it may be really annoying on a build server. To avoid that, you just need to set the wt.headless property:
    mvn test -Dwt.headless  

Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}