|

JBoss Forge – Speedup your enterprise development – Part III Integration Tests with Arquillian


Take your skills to the next level!

The Persistence Hub is the place to be for every Java developer. It gives you access to all my premium video courses, monthly Java Persistence News, monthly coding problems, and regular expert sessions.


In this third part of the JBoss Forge series, I will show you how to search and install plugins to extend the JBoss Forge functionality. After that we will use Forge to setup Arquillian and create an integration test for our webservice.

If you are new to this series, make sure to check the first two parts.
In the first part, we installed JBoss Tools to get JBoss Forge eclipse integration. Then we created a Java EE project with the entities Book and Author and generated a web interface based on these entities.
In the second part, we created a RESTful webservice API for our entities.

Install Plugins

Forge provides several plugins to extend its functionality. Today, we will install the Arquillian plugin which we will use for integration testing. We can use the forge find-plugin test command to get a list of all test related plugins in the  Central Plugin Index (CPI).

[ForgeExample] ForgeExample $ forge find-plugin test

Connecting to remote repository
 [https://raw.github.com/forge/plugin-repository/master/repository.yaml]
... connected!

 - arquillian (org.arquillian.forge:arquillian-plugin:::1.0.0-SNAPSHOT)
    Author: Paul Bakker <[email protected]>
    Website: http://www.jboss.org/arquillian
    Location: https://github.com/forge/plugin-arquillian.git
    Tags: arquillian, jboss, testing, junit, testng, integration testing, 
tests, CDI, java ee
    Description: Integration Testing Framework

 - jacoco (org.jboss.forge.jacoco:plugin-jacoco:::1.0.0-SNAPSHOT)
    Author: Tomas Remes <[email protected]>
    Website: https://github.com/tremes/plugin-jacoco
    Location: https://github.com/tremes/plugin-jacoco.git
    Tags: jacoco, code coverage, java, testing
    Description: This plugin provides installing, running and reporting 
code coverage with JaCoCo.

 - nosqlunit (com.lordofthejars:nosqlunit-forge-plugin:::1.0.0)
    Author: Alex Soto <[email protected]>
    Website: https://github.com/lordofthejars/nosql-unit
    Location: https://github.com/lordofthejars/nosqlunit-forge-plugin
    Tags: test, junit, nosql, persistence tests, nosqlunit
    Description: This plugin provides a way to create tests for NoSQL 
databases using NoSQLUnit .

 - beanstest (de.adorsys.beanstest.plugin:forge-beanstest-plugin:::1.0)
    Author: Christian Brandenstein <[email protected]>
    Website: https://github.com/ersatzcapo/forge-beanstest
    Location: https://github.com/ersatzcapo/forge-beanstest.git
    Tags: test, junit, cdi, mockito
    Description: Enables simple junit testing with CDI beans. 
Also supports mockito mock generation and a test persistence setup with hsqldb.

 - arquillian-extension (org.arquillian.forge:arquillian-extension-plugin:::1.0.0-SNAPSHOT)
    Author: Jérémie Lagarde <[email protected]>
    Website: http://www.jboss.org/arquillian
    Location: https://github.com/jerr/jbossforge-plugin-arquillian-extension.git
    Tags: arquillian, jboss, testing, junit, testng, integration testing, 
tests, drone, graphene, selenium
    Description: Integration Testing Framework Extension for Arquillian Plugin

Forge found five different plugins, hosted in github repositories. We install the arquillian plugin by Paul Bakker. This can be done by calling the forge install-plugin command and passing the name of the plugin:

[ForgeExample] ForgeExample $ forge install-plugin arquillian

JBoss Forge will download the source code and build the plugin during the installation process.

Setup Arquillian

OK, now we can start with our real work for today. We use the Arquillian Forge Plugin to add the required dependencies to our maven configuration.
In the past we would have used another project or the Arquillian Getting Started guide to copy the required maven configuration. With JBoss Forge this is much easier and faster. We just need to setup the Arquillian Forge Plugin and decide which container and container type we want to use. We used JBoss AS 7.1.1 to run our application in part one and two. So we use a JBoss AS7 remote container during the integration tests. After that we need to provide the versions of Arquillian, JUnit and the JBoss AS 7 Arquillian plugin. I selected the latest versions of Arquillian and JUnit and the server plugin for JBoss AS 7.1.1:

[ForgeExample] ForgeExample $ arquillian setup --containerType REMOTE 
--containerName JBOSS_AS_REMOTE_7.X

Which version of Arquillian do you want to install?

  1 - [org.jboss.arquillian:arquillian-bom:pom::1.0.0-SNAPSHOT]
  2 - [org.jboss.arquillian:arquillian-bom:pom::1.0.0.Beta1]
  3 - [org.jboss.arquillian:arquillian-bom:pom::1.0.0.Beta2]
  4 - [org.jboss.arquillian:arquillian-bom:pom::1.0.0.CR1]
  5 - [org.jboss.arquillian:arquillian-bom:pom::1.0.0.CR1-as734-SNAPSHOT]
  6 - [org.jboss.arquillian:arquillian-bom:pom::1.0.0.CR2]
  7 - [org.jboss.arquillian:arquillian-bom:pom::1.0.0.CR3]
  8 - [org.jboss.arquillian:arquillian-bom:pom::1.0.0.CR4]
  9 - [org.jboss.arquillian:arquillian-bom:pom::1.0.0.CR5]
  10 - [org.jboss.arquillian:arquillian-bom:pom::1.0.0.CR6]
  11 - [org.jboss.arquillian:arquillian-bom:pom::1.0.0.CR7]
  12 - [org.jboss.arquillian:arquillian-bom:pom::1.0.0.CR8]
  13 - [org.jboss.arquillian:arquillian-bom:pom::1.0.0.Final]
  14 - [org.jboss.arquillian:arquillian-bom:pom::1.0.1.Final]
  15 - [org.jboss.arquillian:arquillian-bom:pom::1.0.2.Final]
  16 - [org.jboss.arquillian:arquillian-bom:pom::1.0.3.Final]
  17 - [org.jboss.arquillian:arquillian-bom:pom::1.0.4.Final]
  18 - [org.jboss.arquillian:arquillian-bom:pom::1.1.0.Final]
  19 - [org.jboss.arquillian:arquillian-bom:pom::1.1.1.Final]*

 ? Choose an option by typing the number of the selection [*-default]  [0]

Which version of JUnit do you want to install?

  1 - [junit:junit:::3.7]
  2 - [junit:junit:::3.8]
  3 - [junit:junit:::3.8.1]
  4 - [junit:junit:::3.8.2]
  5 - [junit:junit:::3.8.2-brew]
  6 - [junit:junit:::4.0]
  7 - [junit:junit:::4.1]
  8 - [junit:junit:::4.2]
  9 - [junit:junit:::4.3]
  10 - [junit:junit:::4.3.1]
  11 - [junit:junit:::4.4]
  12 - [junit:junit:::4.5]
  13 - [junit:junit:::4.6]
  14 - [junit:junit:::4.7]
  15 - [junit:junit:::4.8]
  16 - [junit:junit:::4.8.1]
  17 - [junit:junit:::4.8.2]
  18 - [junit:junit:::4.9]
  19 - [junit:junit:::4.10]
  20 - [junit:junit:::4.11-beta-1]
  21 - [junit:junit:::4.11]*
  22 - [junit:junit:::4.12-SNAPSHOT]

 ? Choose an option by typing the number of the selection [*-default]  [0]

What version of jboss-as-arquillian-container-remote do you want to use?

  1 - [org.jboss.as:jboss-as-arquillian-container-remote:::7.0.0.Beta1]
  2 - [org.jboss.as:jboss-as-arquillian-container-remote:::7.0.0.Beta2]
  3 - [org.jboss.as:jboss-as-arquillian-container-remote:::7.0.0.Beta3]
  4 - [org.jboss.as:jboss-as-arquillian-container-remote:::7.0.0.CR1]
  5 - [org.jboss.as:jboss-as-arquillian-container-remote:::7.0.0.Final]
  6 - [org.jboss.as:jboss-as-arquillian-container-remote:::7.0.1.Final]
  7 - [org.jboss.as:jboss-as-arquillian-container-remote:::7.0.2.Final]
  8 - [org.jboss.as:jboss-as-arquillian-container-remote:::7.1.0-alrwebinar-SNAPSHOT]
  9 - [org.jboss.as:jboss-as-arquillian-container-remote:::7.1.0.Alpha1-SNAPSHOT]
  10 - [org.jboss.as:jboss-as-arquillian-container-remote:::7.1.0.Beta1]
  11 - [org.jboss.as:jboss-as-arquillian-container-remote:::7.1.0.Beta1b]
  12 - [org.jboss.as:jboss-as-arquillian-container-remote:::7.1.0.CR1]
  13 - [org.jboss.as:jboss-as-arquillian-container-remote:::7.1.0.CR1b]
  14 - [org.jboss.as:jboss-as-arquillian-container-remote:::7.1.0.Final]
  15 - [org.jboss.as:jboss-as-arquillian-container-remote:::7.1.1.Final]
  16 - [org.jboss.as:jboss-as-arquillian-container-remote:::7.1.2.Final]
  17 - [org.jboss.as:jboss-as-arquillian-container-remote:::7.1.3.Final]
  18 - [org.jboss.as:jboss-as-arquillian-container-remote:::7.2.0.Final]*

 ? Choose an option by typing the number of the selection [*-default]  [0] 15

Wrote D:\/dev/wrk4/forge/ForgeExample/pom.xml
Wrote D:\/dev/wrk4/forge/ForgeExample/src/test/resources/arquillian.xml

As you can see in the console output, Forge updated the pom.xml and created the arquillian.xml file for us. If you want to test your application with more than one application server, execute the arquillian setup command again and provide the requested parameter.

Write some tests

After setting up the maven configuration, we can also use Forge to speedup the implementation of the integration tests. The arquillian create-test command generates a test class for the given class, adds the required annotation and provides a first implementation of the ShrinkWrap Deployment. The –enableJPA parameter will add the persistence.xml file to the ShrinkWrap deployment:

[ForgeExample] ForgeExample $ arquillian create-test 
--class blog.thoughts.on.java.forge.rest.AuthorEndpoint.java --enableJPA

The generated source code of our test class looks like this:

@RunWith(Arquillian.class)
public class AuthorEndpointTest
{

   @Inject
   private AuthorEndpoint authorendpoint;

   @Deployment
   public static JavaArchive createDeployment()
   {
      return ShrinkWrap.create(JavaArchive.class, "test.jar")
            .addClass(AuthorEndpoint.class)
            .addAsManifestResource("META-INF/persistence.xml", "persistence.xml")
            .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
   }

   @Test
   public void testIsDeployed()
   {
      Assert.assertNotNull(authorendpoint);
   }
}

As you can see, JBoss Forge injects the class under test, implements a method to create the ShrinkWrap deployment and adds a test method to test the injection. This looks like a nice start to implement our integration tests. But the ShrinkWrap deployment will fail because the list of added classes is incomplete. We need to add the classes Author and Book to the deployment because they are used by the AuthorEndpoint:

@RunWith(Arquillian.class)
public class AuthorEndpointTest
{

   ...

   @Deployment
   public static JavaArchive createDeployment()
   {
      return ShrinkWrap.create(JavaArchive.class, "test.jar")
            .addClasses(AuthorEndpoint.class, Author.class, Book.class)
            .addAsManifestResource("META-INF/persistence.xml", "persistence.xml")
            .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
   }

   ...

}

OK, now we can run our tests with the test command and check if everything is setup correctly. When starting the integration tests, we need to specify the profile which shall be used.

[ForgeExample] ForgeExample $ test --profile arq-jboss_as_remote_7.x

...

Results :

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

[INFO] ------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------

The deployment test was successful and we can implement our real test. As you can see below, I implemented a test of the create() method of the AuthorEndpoint. The implementation of an integration test is simple and straight forward.

@Test
public void createAuthor() {

    // create the author
    Author author = new Author();
    author.setFirstName("J. R. R.");
    author.setLastName("Tolkien");
  
    // add a book
    Set<Book> books = new HashSet<Book>();
    Book book = new Book();
    book.setAuthor(author);
    book.setTitle("Lord of the rings");
    books.add(book);

    author.setBooks(books);

    // store the author in the database
    authorendpoint.create(author);

    // verify
    Assert.assertEquals("Author",(Long) 1L, author.getId());
    Assert.assertEquals("Books", 1, author.getBooks().size());
    Assert.assertEquals("Book",(Long) 2L, author.getBooks().toArray(new Book[1])[0].getId());
}

Eclipse Integration

Executing the integration tests in the JBossForge console is fine, but I also want to do this in my eclipse. This is no problem. We just need to select the maven profile we want to use and we can run our tests in eclipse.

tests

Conclusion

After creating our project in the first and second part, we added Arquillian to our project and implemented an integration test. And again JBossForge took over the annoying parts of the job like configuring maven and creating the initial test classes. I like this much more than copying or trying to remember the required maven configuration. And it’s much faster! What do you think?

We also used the plugin mechanism to extend the JBossForge functionality. There are lots of plugins and as you saw at the beginning of this article, it’s fast and easy to add them to your Forge setup.
If you need something special, you can implement your own plugin. We will do this in one of the upcoming articles about JBossForge.

What do you think about JBossForge? Do you have any experience using it for your test or real projects?
Please write a comment and tell me about it!

Links

 JBossForge Series

2 Comments

  1. Thank you Ronny!

    I think Arquillian is a great framework and a good option to improve the development cycle. For me it feels great to write tests with Arquillian.
    The main benefit of using JBossForge is, that it is easier to use Arquillian. So it's the same as in the first two articles. JBossForge makes it easy to use existing technologies and frameworks 🙂

  2. Is it possible that this is now the end of the nasty write-code, build, deploy, wait-a-while, click-and-enter-some-nasty-things (booooooring!), fix-bugs, re-build, re-deploy… never ending development cycle? I for myself put a lot of hope in both tools. But let's see how it proves in the hard daily practice.

    However, another great article! Thank you very much!

    BTW: Just for completeness. You missed a link to some more of this great stuff: https://github.com/arquillian/continuous-enterprise-development

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.