Skip to main content

Using the ShrinkWrap Maven Resolver for Arquillian Tests

This post assumes that you're familiar with using Arquillian for testing Java applications. If you’re not familiar with Arquillian, then I suggest you check out the guides at http://www.arquillian.org/ where you’ll learn how to write Java tests that can run on the server and are much easier to write and maintain.

When writing an Arquillian test, you create a deployment as in the following code sample:

@Deployment
public static Archive<?> createTestArchive() {
  return ShrinkWrap.create(JavaArchive.class, "test.jar")
    .addClasses(RandomNumberBean.class);
}

It’s not uncommon to see a lot of calls to .addClasses() or .addPackages(). When working with third party libraries, this list of classes/packages added to the archive can grow and grow (and can be a bit of a trial and error process to get a complete list of dependencies). The ShrinkWrap Maven Resolver overcomes this issue by allowing you to specify Maven dependencies in the createTestArchive() method rather than having to add each class/package at a time.

To use the ShrinkWrap Maven Resolver, the first stage is to add the relevant dependency to you Maven project’s pom.xml file.

<dependency>
  <groupId>org.jboss.shrinkwrap.resolver</groupId>
  <artifactId>shrinkwrap-resolver-impl-maven</artifactId>
  <scope>test</scope>
</dependency>

Having configured Maven, you’re ready to go and add dependencies to your Arquillian archive via code as shown in the sample below. In this sample, I’ve added the Apache Commons Math library to the Arquillian archive.

@Deployment
public static Archive<?> createTestArchive() {
  MavenDependencyResolver resolver = DependencyResolvers.use(
  MavenDependencyResolver.class).loadMetadataFromPom("pom.xml");

  return ShrinkWrap
    .create(WebArchive.class, "test.war")
    .addClasses(RandomNumberBeanCommons.class)
    .addAsLibraries(
      resolver.artifact("org.apache.commons:commons-math")
      .resolveAsFiles());
}

Looking at the code, you can see that the first stage is to create a MavenDependencyResolver from the project’s pom.xml file. Then all you need to do, is invoke the .addAsLibraries() method on the Arquillian deployment specifying which Maven dependency to resolve.

Hopefully, you can see that this technique allows you to create your Arquillian tests much faster and more reliably than without using the ShrinkWrap Maven Resolver.

Comments

  1. See also community.jboss.org/.../HowToIAddMavenArtifactsToMyShrinkWrapArchives – which addresses how to use the updated resolver with Arquillian 1.0.0.Final .

    ReplyDelete
  2. And coming soon: New and improved API for ShrinkWrap Descriptors!

    ReplyDelete
  3. …excuse me: ShrinkWrap Resolvers. Descriptors too, but Resolvers *was* the subject of this post.

    ReplyDelete

Post a Comment

Popular posts from this blog

Creating a Joke Application in React

Introduction I've recently started learning React. I've been a backend developer for a long time, but have started to love doing client side work. As they say, the best way to learn is to practise. So here is a simple joke application that I've written in React. The source code for the application can be found on GitHub at: https://github.com/doobrie/react-joke Creating the project Whilst practising, I've quite often created projects from scratch, but this is quite tedious as the same steps need to be taken for each project. I needed to create the project structure, configure Babel, write some control scripts etc. Instead of doing that this time, I've used the create-react-app tool to scaffold the basics of an application. npx create-react-app This sets up everything that you need to get started with a React app. Coding As this is a simple project, I've created one react component, function app() . I've created this as a functional c...

Changing Default Search Provider in Firefox on Linux Mint

On Linux Mint, the default version of Firefox is installed and configured to allow the following search engines to be queried directly from the address bar: Yahoo! Startpage DuckDuckGo Wikipedia Mint defines these as the default available set of search engines based upon 3 criteria (funding to Linux Mint, privacy support and whether the search engine is non-commercial). Other search engines such as Google, Bing or Twitter, etc. can easily be added into the default version of Firefox however. To add a different search provider, browse to Search Engines At the bottom of the page, click on the icon of the requested search engine, then click on the ... button in the URL bar and select the Add Search Engine option. You then have the option to change the default search engine within Firefox preferences to your new choice.

The new Eclipse logo

In a blog post today, Ian Skerrett has announced the new logo for Eclipse. Last fall we [The Eclipse Foundation] started a process to  update the Eclipse logo . The existing logo had not been change since Eclipse was launched and it was showing its age. I am happy to announce we have finished the process and am pleased to introduce the updated Eclipse logo. The new logo has a fresh modern look to it, which I think is a good improvement and will stand Eclipse in good stead for the future. The new logo will be included with the next Eclipse release train, Luna, and gradually rolled out across the Eclipse site and projects. What do you think?  Do you like the new logo?  Add your comments below.