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
- Creating a project with Arquillian JUnit support
- Creating a class
- Developing code for the Greeter class
- Defining javax-inject.jar as a library
- Creating a folder for test sources
- Creating a test class
- Completing the code for the GreteerTest class
- Creating a run configuration for running the test
- Running the test in an embedded container
- Editing the run configuration: adding a managed container
- Creating the arquillian.xml configuration file
- Running the test in a managed container
- Modifying arquillian.xml
- Running the test: deploying to a running server
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
-.
- 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.
- Under Additional Libraries and Frameworks, select the Arquillian JUnit check box.
- Specify the name for your new project (e.g.
HelloArquillian) and click Finish.
When the project is created, you'll see something similar to this in the Project tool window.
(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.
- In the Project tool window, right-click the
srcfolder, point to New and select Java Class.
- In the Create New Class dialog that opens, type
com.example.hello.Greeterin the Name field and press Enter.
The package
com.example.helloand the class
Greeterare shown in the Project tool window.
At the same time, the file
Greeter.javaopens 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.
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.
- In the project root folder, create the folder
lib() and copy
javax-inject.jarinto that folder.
- Open the Project Structure dialog (Ctrl+Shift+Alt+S) and select Libraries.
- Click
, select Java and select
javax-inject.jarin the dialog that opens.
- Click OK in the Choose Modules dialog.
- Click OK in the Project Structure dialog.
Creating a folder for test sources
- In the project root folder, create the folder
test.
- Right-click that folder, point to Mark Directory As and select Test Sources Root.
Creating a test class
- In the editor, place the cursor within the name of the class (
Greeter).
- Click the light bulb
(Alt+Enter) and select Create Test.
- 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.
The new test class is shown in the Project tool window.
At the same time, the file
GreeterTest.javaopens in the editor.
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 ).
Completing the code for the GreteerTest class
Here is the code for the test class in its final state:
package com.example.hello;.inject.Inject; import static org.junit.Assert.*;  greet() throws Exception { String name="Arquillian"; Assert.assertEquals("Hello, Arquillian!", greeter.createGreeting(name)); greeter.greet(System.out, name); } }
- To insert a proper
importstatement for
@Inject, type
@Injand select @Inject (javax.inject).
- Add the remaining code by copying.
Creating a run configuration for running the test
- To the left of
public class GreeterTest, click
and select Run 'GreeterTest'.
- In the Edit configuration dialog that opens, click Configure.
- In the Arquillian Containers dialog, click
, point to Embedded and select GlassFish Embedded 3.1. (We'll start by running the test in an embedded container.)
- In the Edit configuration dialog, select GlassFish Embedded 3.1.
Running the test in an embedded container
- In the Edit configuration dialog, click Run.
The Run tool window opens and, after some time, the test result is shown there.
- Close the Run tool window by clicking
.
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.
- Click the run configuration selector and select Edit Configurations.
- In the Run/Debug Configurations dialog, click Configure.
- In the Arquillian Containers dialog, click
and select Manual container configuration.
- Change the name of the configuration (e.g. to
GlassFish Managed).
- Under Dependencies, click
and select Add maven dependency.
- In the Download Library From Maven Repository dialog, type
arquillian-glassfish-managed-3.1and click
. 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.
At this step, your Arquillian Containers dialog should look something like this:
- In the Run/Debug Configurations dialog, select GlassFish Managed and click OK.
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.
- In the project root folder, create the folder
test-resources.
- Right-click the new folder, point to Mark Directory As and select Test Resources Root.
- In the test-resources folder, create a new file
arquillian.xml.
- Copy the following into the file:
<?xml version="1.0"?> <arquillian xmlns: <container qualifier="glassfish" default="true"> <configuration> <property name="glassFishHome">C:\GlassFish\glassfish4</property> </configuration> </container> </arquillian>
Use your actual path to the GlassFish Server installation folder in place of
C:\GlassFish\glassfish4.
Running the test in a managed container
- To the right of the run configuration selector, click
.
The Run tool window opens and the test result is shown there.
- Close the tool window (
).
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>
Running the test: deploying to a running server
- Start GlassFish Server: select GlassFish and click
.
When the server is started, you'll see something like this in the Run tool window.
- Select GlassFish Managed: GreeterTest and click
.
After some time, the test result is shown in the Run tool window.