2011-10-23

JEE6: Integration testing with JBoss 7

Introduction

When your code utilises some of the JEE6 goodies, such as CDI or EJB, then it may become handy if not necessary to execute the tests for this code on the actual server as opposed to running it in a mocked unit test environment. Benefits?
  • Your tests run in the same environment (or similar) as your production code.
  • In certain cases, it actually makes testing possible, because even when using mocking certain JEE6 behaviours are hard to simulate in a unit test.
JBoss 5 was so slow... But with JBoss 7 it doesn't take so much time to start up the server. So tests can run as a part of your Maven build or inside your IDE.

Configuration

Here I will show you how to set it all up. I did it myself, it works, and you can checkout the source yourself from GitHub.

What are we testing?

First, let's take a look at the class under test.
import javax.ejb.Local;
import javax.ejb.Singleton;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import me.m1key.audiolicious.domain.entities.Song;
import me.m1key.audiolicious.services.SongRepository;

@Singleton
@Local(SongRepository.class)
public class JpaSongRepository implements SongRepository {

   @PersistenceContext
   private EntityManager entityManager;

   @Override
   public void save(Song song) {
       entityManager.persist(song);
   }
}
As you can see, it does not do a lot - but it is an EJB and it uses @PersistenceContext. I did not expose any way to set the entityManager because I don't like having to change production code just so that tests can run. So, that would be rather hard to mock. But we don't have to mock, we are going to run the test on the server.

The test

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.io.IOException;
import java.util.Date;

import javax.inject.Inject;

// Certain imports omitted for brevity.

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ArchivePaths;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.resolver.api.DependencyResolvers;
import org.jboss.shrinkwrap.resolver.api.maven.MavenDependencyResolver;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(Arquillian.class)
public class JpaSongRepositoryIT {

   // Stuff omitted for brevity.
   
   private Date albumDateAdded = new Date();
   private Date albumDateModified = new Date();
   private Date albumDateSkipped = new Date();

   @Inject
   private SongRepository jpaSongRepository;
   @Inject
   private RepositoriesTestHelperBean testHelperBean;

   @Deployment
   public static WebArchive createTestArchive()
           throws IllegalArgumentException, IOException {
       return ShrinkWrap
               .create(WebArchive.class,
                       JpaSongRepositoryIT.class.getSimpleName() + ".war")
               .addAsWebInfResource(EmptyAsset.INSTANCE,
                       ArchivePaths.create("beans.xml"))
               .addAsResource("log4j.xml", "log4j.xml")
               .addAsResource("META-INF/persistence.xml",
                       "META-INF/persistence.xml")
               .addClasses(Album.class, Artist.class, JpaSongRepository.class,
                       NullAlbum.class, NullArtist.class,
                       NullEntitiesFactory.class, Rating.class,
                       RatingTo.class, Song.class, SongRepository.class,
                       SongTo.class, RepositoriesTestHelperBean.class,
                       TrackTo.class)
               .addAsLibraries(
                       DependencyResolvers
                               .use(MavenDependencyResolver.class)
                               .artifacts("org.slf4j:slf4j-api:1.6.1",
                                       "org.slf4j:slf4j-log4j12:1.6.1",
                                       "commons-lang:commons-lang:2.6")
                               .resolveAsFiles());
   }

   @Test
   public void shouldCreateAndRetrieveSong() {
       assertEquals("There should be no songs before any are created.",
               Long.valueOf(0), testHelperBean.totalSongs());

       Artist artist = testHelperBean.createArtist(ARTIST_NAME);
       Album album = testHelperBean.createAlbum(ALBUM_NAME, artist,
               new Rating(80));
       Song song = new Song(SONG_1_NAME, ARTIST_NAME, album, 1988,
               "Zakk Wylde/Bob Daisley/Ozzy Osbourne", "Rock", albumDateAdded,
               albumDateModified, new Rating(80), 9, albumDateSkipped, 0,
               false, 0, 0, false);
       jpaSongRepository.save(song);

       assertNotNull("Saved song should not be null.",
               testHelperBean.getSongByUuid(song.getUuid()));
   }
  
   // Other tests omitted for brevity.
}
Let's take a closer look. First, the test is annotated with @RunWith(Arquillian.class). That is because we need Arquillian to orchestrate the entire test. Arquillian will handle our @Deployment. This is where, using ShrinkWrap, we create the WAR file with our test and what we want to test with all its dependencies, also the external ones, from our pom.xml. This WAR file will be deployed to JBoss 7 when you run the test with Maven or from within your IDE. I must admit that getting the dependencies right (i.e. giving it exactly what it needs and not too much) can be a bit tiresome and frustrating.

Dependencies

Below are the relevant dependencies.
       <dependency>
           <groupId>javax.inject</groupId>
           <artifactId>javax.inject</artifactId>
           <scope>provided</scope>
       </dependency>
       <dependency>
           <groupId>org.jboss.spec</groupId>
           <artifactId>jboss-javaee-web-6.0</artifactId>
           <type>pom</type>
           <scope>provided</scope>
       </dependency>
       <dependency>
           <groupId>org.jboss.as</groupId>
           <artifactId>jboss-as-arquillian-container-managed</artifactId>
           <scope>test</scope>
       </dependency>
       <dependency>
           <groupId>org.jboss.shrinkwrap</groupId>
           <artifactId>shrinkwrap-api</artifactId>
           <scope>test</scope>
       </dependency>
       <dependency>
           <groupId>org.jboss.shrinkwrap.resolver</groupId>
           <artifactId>shrinkwrap-resolver-impl-maven</artifactId>
           <scope>test</scope>
       </dependency>
       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <scope>test</scope>
       </dependency>
       <dependency>
           <groupId>org.jboss.arquillian.junit</groupId>
           <artifactId>arquillian-junit-container</artifactId>
           <scope>test</scope>
       </dependency>

arquillian.xml

You also need to provide an arquillian.xml file in your src/test/resources. Below is my entire file.
<?xml version="1.0" encoding="UTF-8"?>
<arquillian xmlns="http://jboss.org/schema/arquillian"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://jboss.org/schema/arquillian
       http://jboss.org/schema/arquillian/arquillian_1_0.xsd">

   <!-- Uncomment to have test archives exported to the file system for inspection -->
   <engine>
       <property name="deploymentExportPath">target/</property>
   </engine>

   <container qualifier="jboss" default="true">
       <protocol type="jmx-as7">
           <property name="executionType">REMOTE</property>
       </protocol>
   </container>

</arquillian>
When the test is running, it's running on a remote instance of JBoss. That means you have to download JBoss 7 yourself. You can hardcode its location in this file, but it's not a good idea if you're sharing the project with other people. Instead, you can set your JBOSS_HOME system variable to something like e:\Servers\jboss-as-web-7.0.0.Final.

Download sample source code

As always, working code is available for you to download. Get it now from GitHub. There is a text file called SETUP that explains how to get it going (the database and so on). If you have any questions, feel free to post them here and I will do my best to answer.

15 comments:

  1. Ha! I forgot to mention I am publishing this post from the future.

    Fixed, thanks. :)

    ReplyDelete
  2. Ah, so you bought this one? http://otomoto.eu/de-lorean-dmc-12-powrot-do-przyszlosci-C20647286.html

    ReplyDelete
  3. I can't tell you without causing a serious paradox! Sorry. :) Unless the other quantum interpretation is right... Hmm...

    ReplyDelete
  4. If you are a beginner, a java code helper could be your best friend. Whenever you want to learn codes or if you are looking for an idea what to put in your program, you can easily take ideas from this documentation. Here you will find all the details regarding the software you are working on. You can borrow ideas and codes as per your requirements. It is like your java assignment helper. Sometimes, the documentation holds so much information that the user gets flabbergasted. In such a situation, you can always search the internet for any kind of information or hint! So search the internet for any kind of java assignment help.

    ReplyDelete
  5. Nice post. Thanku so much to share this with us.

    putlocker
    kissanime

    ReplyDelete
  6. Thanku for posting I will also refer this to someone, to know more about  Best Hrms software in india

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. Hello! It's an interesting and motivating write-up. I truly appreciate the amazing work that you have done in creative and smart information to share with us. Many thanks for sharing. mouau postgraduate admission form

    ReplyDelete
  9. What a great blog website! Keep up the good work; you done a terrific job of touching on everything that matters to all readers. And your blog pays far more attention to details. I really appreciate you sharing.
    Abogados Divorcio Virginia

    ReplyDelete
  10. Hi
    Thanks for sharing an amazing and informative post. The information shared by you is really useful for me. Keep it up to do great work and hope to see more of your posts in the near future.
    read more :[pii_email_48b2037260d5568d09f4]

    ReplyDelete
  11. This article explores the key facets of Group Personal Accident Insurance, its benefits, and why it should be considered an essential component of comprehensive employee welfare programs.


    visit site :

    Group Personal Accident Insurance

    ReplyDelete
  12. Embark on a journey of international opportunities with FlyHigh Abroad, your trusted guide for realizing aspirations. Explore seamless pathways to obtaining a work permit in Canada and delve into the possibilities of pursuing a European education without the need for IELTS. Let FlyHigh Abroad be your compass in navigating the complexities of global mobility, ensuring your dreams take flight effortlessly. Discover the expertise that sets FlyHigh Abroad apart in facilitating work permits and opening doors to education in Europe study visa without IELTS. Your global journey begins with FlyHigh Abroad, where possibilities are limitless.

    ReplyDelete

  13. Welcome to the ultimate guide on mastering implied odds poker Whether you're a seasoned player or just starting, understanding implied odds is crucial for success in this dynamic game of skill and strategy. In this comprehensive article, we'll delve into the intricacies of implied odds poker, exploring key concepts, advanced strategies, and practical tips to elevate your gameplay to the next level. Let's dive in!Implied odds poker is a sophisticated strategy that goes beyond the basic odds of winning a hand. It involves assessing the potential future bets and factoring them into the current decision-making process. By understanding implied odds, players can make more informed decisions about whether to call, raise, or fold, ultimately maximizing their profitability over the long term.Mastering implied odds begins with understanding how to calculate them accurately. Unlike pot odds, which consider the current size of the pot relative to the bet size, implied odds take into account the potential size of future bets. This involves estimating the likelihood of winning a hand and predicting the potential profit from future bets if you hit your draw.

    Read more : ofilmywap

    ReplyDelete
  14. With the top immigration consultants in Dubai on your side, start your adventure to Europe. At FlyHigh Abroad, we take great satisfaction in being the best consultancy in dubai for europe, providing unmatched knowledge and individualized support to help you realize your European aspirations. Our team of seasoned professionals is available to assist you at every stage of your journey, whether you are looking for family sponsorship, skilled migration, or educational possibilities. You can rely on FlyHigh Abroad to expertly and precisely handle the complications of European immigration.

    ReplyDelete