Home » Java » Enterprise Java » Develop A Microservice with Forge, WildFly Swarm and Arquillian

About Alex Soto

Alex Soto

Develop A Microservice with Forge, WildFly Swarm and Arquillian

In this post we are going to see how to develop a microservice using WildFly Swarm and Forge and testing it with Arquillian and Rest Assured.
WildFly Swarm offers an innovative approach to packaging and running Java EE applications by packaging them with just enough of the server runtime to “java -jar” your application.

JBoss Forge is a software development tool that extends your Java IDE, providing wizards and extensions (add-ons) for different technologies and solutions.

Arquillian is a platform that simplifies integration testing for Java middleware. It deals with all the plumbing of container management, deployment, and framework initialization so you can focus on the task of writing your tests—real tests.

REST Assured brings the simplicity of testing and validating REST services in dynamic languages such as Ruby and Groovy into the Java domain.

http://downloads.jboss.org/forge/releases/3.4.0.Final/forge-distribution-3.4.0.Final-offline.zip or navigate to http://forge.jboss.org/download and download the plugin for Eclipse, Netbeans or IntelliJ. For this example, I am going to use the CLI one.
After you’ve installed Forge and it is available in PATH environment variable you can start working on it.

First of all go to the directory where you want to store the project and run forge. After a few seconds, you’ll see that Forge is started and you are ready to type commands:


After that you need to install the wildfly-swarm addon. To do it just type next command on Forge shell:

> addon-install-from-git --url https://github.com/forge/wildfly-swarm-addon

Then the latest addon will be downloaded and installed. After this setup step, you can start creating your microservice by calling:

> project-new --top-level-package org.superbiz --named foo --type wildfly-swarm

This command creates a new project called foo, with pom.xml prepared with all wildfly swarm requirements. Next step is adding a wildfly swarm fragment. A fragment is a way to define which modules you want to be able at runtime.

> wildfly-swarm-add-fraction --fractions microprofile

In this case microprofile fraction is added. This means that at runtime CDI + JSON-P + JAXRS will be available.Addon also creates a JAX-RS endpoint as an example, you can check it by running next two commands:

> cd src/main/java/org/superbiz/rest/HelloWorldEndpoint.java
> ls

Then return to root of the project and let’s call the command that creates an Arquilian test for the microservice

> wildfly-swarm-new-test --target-package org.superbiz --named HelloWorldEndpointTest --as-client

In this case the test is called HelloWorldEndpointTest and test is going to run in Arquillian as-client mode (which means that the test is not deployed inside the container and will be run at local runtime). You can check the generated code with next two commands:

> cd src/test/java/org/superbiz
> cat HelloWorldEndpointTest.java

Notice that test does not validate nothing yet, but since we are using as-client mode, the test injects the URL where the application is started. Let’s add some checks using REST-assured.Return to the root of the project and add REST-assured dependency by calling next command:

> project-add-dependencies io.rest-assured:rest-assured:3.0.1:test
> cat pom.xml

Finally you can use REST-assured in empty test to validate that your microservice endpoint effectively returns “Hello from WildFly Swam!”.

package org.superbiz;

import io.restassured.builder.RequestSpecBuilder;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.wildfly.swarm.arquillian.DefaultDeployment;

import java.net.URISyntaxException;
import java.net.URL;

import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.is;

@RunWith(Arquillian.class)
@DefaultDeployment(testable = false, type = DefaultDeployment.Type.WAR)
public class HelloWorldEndpointTest {

  @ArquillianResource
  private URL url;

  @Test
  public void should_start_service() throws URISyntaxException {

    RequestSpecBuilder requestSpecBuilder = new RequestSpecBuilder();
    requestSpecBuilder.setBaseUri(url.toURI());
    given(requestSpecBuilder.build())
	.when()
        .get("hello")
	.then()
        .assertThat()
	.body(is("Hello from WildFly Swarm!"));

  }
}

When you run this test, what it is happening behind the scene is that the microservice is packaged and deployed locally. When service is ready to receive incoming requests, then the test will send a GET request to /hello and asserts that the response body is “Hello from WildFly Swam!”
You can see this running at https://youtu.be/9xb6GIZ1gjs
This is a really simple example, and this was the intention of this post. Just show you how using Forge and just running some commands you get an started project with its integration test running.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz