IntelliJ IDEA 2017.1 Help

Arquillian: a Quick Start Guide

This feature is supported in the Ultimate edition only.

This guide shows main IntelliJ IDEA features for writing and running Arquillian tests.

Before you start

Make sure that the following software is installed on your computer:

  • IntelliJ IDEA ULTIMATE Edition. Also check if the JBoss Arquillian Support plugin is enabled.
  • Java SE Development Kit (JDK), version 8. Download Oracle JDK.
  • GlassFish Server, version 4. The server will be used as a managed Arquillian container. Download GlassFish.

You should also download javax-inject.jar. This file will be used as a library when developing our sample test class.

Creating a project with Arquillian JUnit support

  1. Click Create New Project on the Welcome screen, or select File | New | Project.

    The New Project wizard opens.

  2. In the left-hand pane, select Java Enterprise.
  3. If the JDK that you want to use is already defined in IntelliJ IDEA, select that JDK from the Project SDK list. Otherwise, click New, select JDK, and select the JDK installation folder in the dialog that opens.
  4. If GlassFish is not defined in IntelliJ IDEA yet, click New to the right of the Application Server field and select Glassfish Server.

    In the Glassfish Server dialog, specify the GlassFish Server installation directory.

  5. Under Additional Libraries and Frameworks, select the Arquillian JUnit check box.
    /help/img/idea/2017.1/arq01NewProject.png

    Click Next.

  6. Specify the name for your new project (e.g. HelloArquillian) and click Finish.
    /help/img/idea/2017.1/arq02NewProject.png

    When the project is created, you'll see something similar to this in the Project tool window.

    /help/img/idea/2017.1/arq03Project.png

(To add Arquillian JUnit support for an existing project: in the Project tool window, right-click your project or module folder and select Add Framework Support. Then, select the Arquillian JUnit check box in the dialog that opens.)

Creating a class

Now we are going to create a class that we'll test. Let the class name be com.example.hello.Greeter.

  1. In the Project tool window, right-click the src folder, point to New and select Java Class.
  2. In the Create New Class dialog that opens, type com.example.hello.Greeter in the Name field and press Enter.

    The package com.example.hello and the class Greeter are shown in the Project tool window.

    /help/img/idea/2017.1/arq04ProjectGreeter.png

    At the same time, the file Greeter.java opens in the editor.

Developing code for the Greeter class

Here is the code for the Greeter class.

package com.example.hello; import java.io.PrintStream; public class Greeter { public void greet(PrintStream to, String name) { to.println(createGreeting(name)); } public String createGreeting(String name) { return "Hello, " + name + "!"; } }

Copy the code into the editor.

/help/img/idea/2017.1/arq05EditorGreeter.png

Defining javax-inject.jar as a library

To be able to write and run our Arquillian test, we need javax-inject.jar as a library.

  1. In the project root folder, create the folder lib (New | Directory) and copy javax-inject.jar into that folder.
    /help/img/idea/2017.1/arq06ProjectJavaxInject.png
  2. Open the Project Structure dialog (Ctrl+Shift+Alt+S) and select Libraries.
  3. Click /help/img/idea/2017.1/new.png, select Java and select javax-inject.jar in the dialog that opens.
  4. Click OK in the Choose Modules dialog.
    /help/img/idea/2017.1/arq07LibrariesJavaxInject.png
  5. Click OK in the Project Structure dialog.

Creating a folder for test sources

  1. In the project root folder, create the folder test.
  2. Right-click that folder, point to Mark Directory As and select Test Sources Root.
    /help/img/idea/2017.1/arq08ProjectTest.png

Creating a test class

  1. In the editor, place the cursor within the name of the class (Greeter).
  2. Click the light bulb /help/img/idea/2017.1/intentionBulb.png (Alt+Enter) and select Create Test.
    /help/img/idea/2017.1/arq09GreeterCreateTest.png
  3. In the Create Test dialog that opens, select Arquillian JUnit4 from the Testing library list. Under Create test methods for, select the method that doesn't return a value (greet). Click OK.
    /help/img/idea/2017.1/arq10DialogCreateTest.png

    The new test class is shown in the Project tool window.

    /help/img/idea/2017.1/arq11ProjectGreeterTest.png

    At the same time, the file GreeterTest.java opens in the editor.

    /help/img/idea/2017.1/arq12EditorGreeterTest.png

    The initial content of the test class is defined by the corresponding template. You can edit that template on the Code tab of the File and Code Templates page in the Settings / Preferences dialog (Ctrl+Alt+S | Editor | File and Code Templates).

Completing the code for the GreteerTest class

Here is the code for the test class in its final state:

package com.example.hello; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit.Arquillian; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.asset.EmptyAsset; import org.jboss.shrinkwrap.api.spec.JavaArchive; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import javax.inject.Inject; import static org.junit.Assert.*; @RunWith(Arquillian.class) public class GreeterTest { @Deployment public static JavaArchive createDeployment() { return ShrinkWrap.create(JavaArchive.class) .addClass(Greeter.class) .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); } @Inject Greeter greeter; @Test public void greet() throws Exception { String name="Arquillian"; Assert.assertEquals("Hello, Arquillian!", greeter.createGreeting(name)); greeter.greet(System.out, name); } }
  1. To insert a proper import statement for @Inject, type @Inj and select @Inject (javax.inject).
    /help/img/idea/2017.1/arq13EditorGreeterTestInject.png
  2. Add the remaining code by copying.
    /help/img/idea/2017.1/arq14EditorGreeterTestComplete.png

Creating a run configuration for running the test

  1. To the left of public class GreeterTest, click /help/img/idea/2017.1/runTwoGreenArrows.png and select Run 'GreeterTest'.
    /help/img/idea/2017.1/arq15EditorRunGreeterTest.png
  2. In the Edit configuration dialog that opens, click Configure.
    /help/img/idea/2017.1/arq16EditConfiguration.png
  3. In the Arquillian Containers dialog, click /help/img/idea/2017.1/new.png, point to Embedded and select GlassFish Embedded 3.1. (We'll start by running the test in an embedded container.)
    /help/img/idea/2017.1/arq17ArquillianContainers.png
  4. In the Edit configuration dialog, select GlassFish Embedded 3.1.
    /help/img/idea/2017.1/arq18EditConfiguration.png

Running the test in an embedded container

  1. In the Edit configuration dialog, click Run.

    The Run tool window opens and, after some time, the test result is shown there.

    /help/img/idea/2017.1/arq19TestResult.png
  2. Close the Run tool window by clicking /help/img/idea/2017.1/close.png.

Editing the run configuration: adding a managed container

Now let's change our run configuration so that it could be used for running the test in a managed container.

  1. Click the run configuration selector and select Edit Configurations.
    /help/img/idea/2017.1/arq20EditConfigurations.png
  2. In the Run/Debug Configurations dialog, click Configure.
    /help/img/idea/2017.1/arq21EditConfigurationsDialog.png
  3. In the Arquillian Containers dialog, click /help/img/idea/2017.1/new.png and select Manual container configuration.
  4. Change the name of the configuration (e.g. to GlassFish Managed).
  5. Under Dependencies, click /help/img/idea/2017.1/new.png and select Add maven dependency.
  6. In the Download Library From Maven Repository dialog, type arquillian-glassfish-managed-3.1 and click /help/img/idea/2017.1/find.png. Then select org.jboss.arquillian.container:arquillian-glassfish-managed-3.1:1.0.0.CR4 from the list.

    Select the Download to check box and click OK.

    /help/img/idea/2017.1/arq22DownloadLibraryFromMaven.png

    At this step, your Arquillian Containers dialog should look something like this:

    /help/img/idea/2017.1/arq23ArquillianContainers.png

    Click OK.

  7. In the Run/Debug Configurations dialog, select GlassFish Managed and click OK.
    /help/img/idea/2017.1/arq24RunConfigurationsDialog.png

Creating the arquillian.xml configuration file

To be able to run an Arquillian test in a managed container, the container adapter needs to know the container location. So let's create the arquillian.xml configuration file with the necessary info.

  1. In the project root folder, create the folder test-resources.
  2. Right-click the new folder, point to Mark Directory As and select Test Resources Root.
  3. In the test-resources folder, create a new file arquillian.xml.
    /help/img/idea/2017.1/arq25ProjectArquillianXml.png
  4. Copy the following into the file:
    <?xml version="1.0"?> <arquillian xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://jboss.org/schema/arquillian" xsi:schemaLocation="http://jboss.org/schema/arquillian http://jboss.org/schema/arquillian/arquillian_1_0.xsd"> <container qualifier="glassfish" default="true"> <configuration> <property name="glassFishHome">C:\GlassFish\glassfish4</property> </configuration> </container> </arquillian>
    /help/img/idea/2017.1/arq26EditorArquillianXml.png

    Use your actual path to the GlassFish Server installation folder in place of C:\GlassFish\glassfish4.

Running the test in a managed container

  1. To the right of the run configuration selector, click /help/img/idea/2017.1/run.png.
    /help/img/idea/2017.1/arq27GlassFishManagedRun.png

    The Run tool window opens and the test result is shown there.

    /help/img/idea/2017.1/arq28TestResult.png
  2. Close the tool window (/help/img/idea/2017.1/close.png).

Modifying arquillian.xml

Sometimes, you need to deploy your test to a container that is already running. In such cases, you can just change the container configuration file a little and continue using the managed container adapter.

Add the following to arquillian.xml:

<property name="allowConnectingToRunningServer">true</property>
/help/img/idea/2017.1/arq29EditorArquillianXml.png

Running the test: deploying to a running server

  1. Start GlassFish Server: select GlassFish and click /help/img/idea/2017.1/run.png.
    /help/img/idea/2017.1/arq30SelectGlassFish.png

    When the server is started, you'll see something like this in the Run tool window.

    /help/img/idea/2017.1/arq31GlassFishStarted.png
  2. Select GlassFish Managed: GreeterTest and click /help/img/idea/2017.1/run.png.
    /help/img/idea/2017.1/arq32SelectGreeterTest.png

    After some time, the test result is shown in the Run tool window.

    /help/img/idea/2017.1/arq33TestResult.png

See Also

Last modified: 18 July 2017