Tag Archive for: Java

I’m excited about this new library I ran into and I would like to share the excitement. It’s called Restito, it’s on GitHub, and I actually found it on a blog from 2012, so… where were you all this time?

Restito is a Java library to mock out REST APIs. It’s self-described as the complement of REST Assured. REST Assured is also a library used for testing REST APIs (like Restito), but it mimics the client (while Restito mimics the server), it has a fluent API (like Restito), and it’s very popular (no shame in aspiring for popularity). In fact, I see REST Assured as the gold standard for testing REST APIs. Restito, on the other hand, is new to me.

Restito

Problem

What problem are we trying to solve here? We are building a service that’s chatting with a remote instance of our Jama application, using Jama’s REST API.

Restito

Inside the service there is a whole chain of classes, that are dependent on each other. There is a class JamaProxy, which has some business level methods in the context of our Jama application. It calls into JamaRestClient, which understands how to form request URLs and payloads compliant with Jama’s REST API. It uses javax.ws.rs.client.WebTarget and friends to set up the actual connection with the remote Jama application.

Restito

Concerns are separated, and when it comes to unit testing, each concern can also be unit tested separately. IntegrationHandler has a corresponding IntegrationHandlerTest, which uses a mocked version of JamaProxy. The same for JamaProxy, which uses a mocked version of JamaRestClient. But how do we unit test JamaRestClient?

JamaRestClient is strongly tied to WebTarget (in fact, its only purpose in life is transformation into and out from WebTarget); it makes no sense to abstract WebTarget out of JamaRestClient. I have tried to mock WebTarget in unit tests before, and found it notoriously hard to mock (hard meaning annoying and ugly). The way a lot of its method calls are chained (fluent API, builder pattern) doesn’t lead to very pretty unit test code. Also, mocking out a request library is scary because there are so many ways you can end up with a mock that doesn’t behave like the real thing.

It would be more realistic if we used real communication to the real REST API. It would allow us to use the real request library. Restito lets us use real communication, without the need to set up a real Jama application to talk to. (If you are going to tell me that this is not real unit testing, please talk to the hand: we have narrowed the scope of the test to the smallest unit that makes sense, and we’re doing it in a way that increases confidence in our product.)

When it comes to integration testing, you might have the same problem. Depending on how you define integration testing, it may be undesirable to create real Jama application to communicate with. Restito can help in the same way as for unit testing. It’s not a full substitute for “the real thing”, so we likely desire a system test that involves a real Jama application, but Restito will help boost confidence in earlier phases of development.

Including Restito

Restito is available as a Maven artifact, so you add it to the POM file as follows:

<dependencies>  
...  
    <dependency>  
        <groupId>com.xebialabs.restito</groupId>  
        <artifactId>restito</artifactId>  
        <version>0.8.2</version>  
        <scope>test</scope>  
    </dependency>

Your POM would also have dependencies on the client library, for example javax.ws.rs-api for WebTarget, but that’s beside the point here.

How It Works

Restito sets up a little web server, on a random available port. It is then possible to set up expectations, before exercising the test subject. Then even verifications can be done to make sure that calls to the REST API actually happened, in the way expected. Here is some example code — some non-essential constants and methods, as well as imports omitted for brevity:

public class JamaRestClientTest {  
    private StubServer server = new StubServer();  
    private ObjectMapper objectMapper = new ObjectMapper();  
  
    @Before  
    public void setUp() {  
        server.run();  
    }  
  
    @Test  
    public void testGetItem() throws JsonProcessingException {  
        Item expected = someItem(SOME_ITEM_ID);  
  
        whenHttp(server)  
                .match(get(format("/contour/rest/v1/items/%d", SOME_ITEM_ID)))  
                .then(ok(), jsonContent(expected), contentType("application/json"));  
  
        JamaRestClient subject
            = new JamaRestClient(baseUrl(), MY_USER_NAME, MY_PASSWORD, null);  
        Item actual = subject.get("items", SOME_ITEM_ID, Item.class);  
  
        assertEquals(SOME_ITEM_ID, actual.getId());  
    }  
  
    private Action jsonContent(Object object) throws JsonProcessingException {  
        return stringContent(objectMapper.writeValueAsString(object));  
    }  
  
    private String baseUrl() {  
        return format("http://localhost:%d/contour", server.getPort());  
    }  
}

Some things to note in the above code fragment: I have also introduced some usage of Jackson’s ObjectMapper, so that I don’t need to deal with literal JSON here, but can just pass an object in. I’ve not seen support for that in Restito, and I find that unfortunate.

The code is fairly easy to read, thanks to the fluent API of Restito, and without the need to mock out all the nested objects returned by WebTarget’s fluent API. I find it interesting how that almost sounds at odds: using a nice fluent API for a mock because fluent APIs are so hard to mock.

An example of a verification that could be added to the above example would be like this:

verifyHttp(server)  
    .once(method(Method.GET),
        parameter("documentKey", "DOC-KEY-123"),
        not(withHeader("x-some-header")));

The above code fragment shows how we verify that the server actually received an HTTP GET request once, with a certain query parameter, and specifically without a header called x-some-header.

Conclusion

I observe that Restito does not have the richness and cleanness on its fluent API, as REST Assured does, which it seems to aspire to. But I’ve fallen in love with it nonetheless, because I can see the void it’s filling. I was able to create a working test with Restito in under 15 minutes from the moment I started Googling. (It actually was 14 minutes, I time myself.) I know I will be using it a lot in the future.

Monitoring Java ApplicationsJava developers need to monitor and trouble-shoot Java applications from time to time. It’s usually straightforward to do that if an applications is running as a Java processes on your local machine. But when the Java application runs inside a Docker container on a Docker host, it becomes challenging to monitor them using tools running locally, even when the Docker host is just a virtual machine running on your desktop. In this blog post, I will describe a couple of ways to monitor such Java applications.

Connect VisualVM to an Application through JMX agents

VisualVM is a GUI tool that monitors and profiles a JVM. It comes with JDK and can be start by running “jvisualvm” command. You can also install it as a separate application. The tool connects to local Java processes directly, but in order to connect to a Java application running inside a Docker container, it needs to connect through a JMX port.

To enable remote JMX connection, you need to run your Java application in the Docker container with JVM options like these:

-Dcom.sun.management.jmxremote.rmi.port=9090

-Dcom.sun.management.jmxremote=true

-Dcom.sun.management.jmxremote.port=9090 

-Dcom.sun.management.jmxremote.ssl=false

-Dcom.sun.management.jmxremote.authenticate=false

-Dcom.sun.management.jmxremote.local.only=false

-Djava.rmi.server.hostname=192.168.99.100

Where java.rmi.server.host.name specifies the host name or IP address that your JMX client uses to connect to the target Java application.

Make sure to publish container’s port 9090 as the Docker host port 9090 when starting the Docker container:
$ docker run -p 9090:9090 {your_docker_image}

If your Docker host runs behind a firewall, you could use a SSH tunnel to connect to port 9090 of your Docker host. This time, you may need to set your RMI server host name to be localhost:
-Djava.rmi.server.hostname=localhost

Then ssh to Docker host using an SSH tunnel:
$ ssh -L 9090:localhost:9090 {your_docker_host}

This will forward the connection from your local port 9090 to localhostport 9090 on your Docker host server.

As a best practice, these JVM arguments should be passed as an environment variable that could be used by an entry script to start the Java application. For example:
$ docker run -p 9090:9090 -e MY_JAVA_OPTS=”…”  {your_docker_image}

Once your application is running with the the right options, you can connect your VisualVM to the application with the following steps:

1. Run VisualVM
$ jvisualvm

2. Add a remote host

Sean 1

3. Add a JMX connection
2 sean

3. Click on your JMS connection to connect to your application
sean 3

Running Java Mission Control and Java Flight Recorder

Java Mission Control (JMC) is a monitoring and performance tool offered by Oracle as a commercial feature of JDK 7 and 8. A key feature of JMC is Java Flight Recorder (JFR) that can be used to record event history for performance diagnosis and tuning. JMC is free to use for development.

To enable Java Mission Control, specify the following JVM options when starting your Java application in the Docker container, in addition to the same JMX-related options described above:

-XX:+UnlockCommercialFeatures

-XX:+FlightRecorder

Here are the steps to connect your JMC GUI client to a Java application that that has JMC/JFR feature enabled:

1. Run the Java Mission Control GUI that comes with Java JDK 7 or 8:
$ jmc

2. Set up a remote connection
sean 4 sean 5 sean 6

 

4. Click on 192.168.99.100:9090, then “MBean Server”, to monitor the application, or click on “192.168.99.100:9090” to start a flight recording session.
sean 7


RELATED POST: Checklist: Selecting a Requirements Management Tool


Setting up New Relic

New Relic is a hosted solution for application monitoring. To enable New Relic for your Java application, you need to have a New Relic account and install New Relic Java agent with your application. Here are the key steps to set it up. For more details, see the New Relic Documentation.

1. Download New Relic Agent ZIP file from https://docs.newrelic.com/docs/agents/java-agent/installation/install-java-agent.

2. Unzip the downloaded file. The unzipped file folder should contain files newrelic.jar and newrelic.yml

3. Edit newrelic.ymlfile to configure your license key and application name

license_key: ‘{your_newrelic_license_key}’

app_name: ‘{your_app_name}’

4. Include the unzipped folder in your Docker image

5. Start your Java application inside the container with the following JVM option:
-javaagent:/path/to/newrelic.jar

6. Log in to your New Relic account and your should see your application shows up in the application list.

Debug Java Application Remotely

To debug your Java application remotely using your favorite IDE, use the following JVM option to start your application assuming the debugging agent is running at port 5005:

-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005

Make sure your publish port 5005 to your Docker host when starting your Docker container.

As an example, if you use IntelliJ as your Java IDE to debug, you can start by opening the project that contains the source code of your Java application.

Then, use “Run/Debug Configuration” Dialog to create a remote run/debug configuration.
sean 8

Now you can run “MyRemoteApp” application configuration to start remote debugging.

Summary

In this blog post, I have described how to configure VisualVM, JMC, New Relic, and Java remote debugging to monitor and profile your Java applications that runs inside Docker containers. I hope you find this information helpful!


Learn more about how Jama Connect streamlines tracking and tracing requirements.

SEE THE SOLUTION