Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Developing Microservices With Forge and WildFly Swarm

DZone's Guide to

Developing Microservices With Forge and WildFly Swarm

By simply using Forge and just running a couple of commands, you can start a project with its integration test up and running.

· Integration Zone
Free Resource

Learn how API management supports better integration in Achieving Enterprise Agility with Microservices and API Management, brought to you in partnership with 3scale

In this post, we are going to see how to develop a microservice using WildFly Swarm and Forge and test 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 java-jar to 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.

The first thing you need to do is install Forge. You can just download the CLI console from this link or navigate to here 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 the 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 so, just type the following command on the Forge shell:

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

Next, the latest add-on 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. The 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, the microprofile fraction is added. This means that at runtime, CDI, JSON-P, and JAXRS will be available.

This 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 the root of the project. 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 it is going to run in Arquillian in 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 has not validated anything 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 the 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 an 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 here.  This is a really simple example, and this was the intention of this post: to show you that by using Forge and just running some commands, you can start a project with its integration test running.

Unleash the power of your APIs with future-proof API management - Create your account and start your free trial today, brought to you in partnership with 3scale.

Topics:
forge ,arquillian ,microservices ,wildfly swarm ,rest assured ,integration

Published at DZone with permission of Alex Soto, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}