Over a million developers have joined DZone.

Container Object Pattern, A New Pattern for Your Tests

How to use container object patterns in your Arquillan Cube tests for Java applications.

· DevOps Zone

The DevOps zone is brought to you in partnership with Sonatype Nexus. The Nexus suite helps scale your DevOps delivery with continuous component intelligence integrated into development tools, including Eclipse, IntelliJ, Jenkins, Bamboo, SonarQube and more. Schedule a demo today

If you search for a description of what Page Object is, you’ll find that The Page Object Pattern gives us a common sense way to model content in a reusable and maintainable way.

Within your web app’s UI there are areas that your tests interact with. A Page Object simply models these as objects within the test code. This reduces the amount of duplicated code and means that if the UI changes, the fix need only be applied in one place.

As you can see, Page Object applies to UI elements. We (the Arquillian community) has coined a new pattern following Page Object pattern logic called Container Object pattern. You can think about Container Objects as the area of a container (like a Docker container) that your test might interact with. For example, some of these areas could be:

  • To get the host IP where container is running.
  • The bounded port for a given exposed port.
  • Any parameter configured inside the configuration file (Dockerfile) like a user or password to access to the service which the container exposes.
  • Definition of the containers.

A Container Object might contain an aggregation of more than one Container Object inside it. This effectively builds a relationship (link) between containers.

An example of configuration parameters might be, for example, if running a MySQL database in a container, it could be the user and password to access to database. 

Notice that nothing prevents you to generate the correct URL for accessing to the service from the test, or execute commands against the container like retrieving an internal file.

And of course as Page Object does, Container Object gives you a way to build a model content that can be reused for several projects.

Before looking at how this pattern is implemented in Arquillian Cube, let’s go thorough an example: Suppose all of your applications need to send a file to an FTP server. To write an integration/component test you might need a FTP server to send the file and check that the file was correctly sent.

One way to do this is using Docker to start a FTP server just before executing the test, then execute the test using this Docker container for FTP server, before stopping the container check that the file is there, and finally stop the container. So all these operations that involves the FTP server and container could be joined inside a Container Object. This container object might contain information like:

  • Which image is used,
  • IP and bounded port of host where this FTP server is running,
  • Username and password to access to the FTP server, and
  • Methods for asserting the existence of a file.

Then from the point of view of the test, it only communicates with this object instead of directly hard coding all information inside the test.

Again as in Page Object, any change on the container only affects the Container Object and not the test itself. Now let’s see how Arquillian Cube implements Container Object pattern with a very simple example: Arquillian Cube and Container Object. Let’s see a simple example on how you can implement a Container Object in Cube. Suppose you want to create a container object that encapsulates a ping pong server running inside Docker.

The Container Object will be like a simple POJO with special annotations:

@Cube(value = "pingpong", portBinding = "5000->8080/tcp") // <1>
public class PingPongContainer {

  @HostIp // <2>
  String dockerHost;

  @HostPort(8080) // <3>
  private int port;

  public URL getConnectionUrl() { // <4>
    try {
      return new URL(“http://” + dockerHost + “:” + port);
	  } catch (MalformedURLException e) {
		  throw new IllegalArgumentException(e);

In the example, you must pay attention to these lines:

  1. @Cube annotation configures Container Object.
  2. A Container Object can be enriched with Arquillian enrichers.
  3. Bounded port is injected for given exposed port.
  4. Container Object hides how to connect to PingPong server.

@Cube annotation is used to configure this Container Object. Initially you set that the started container will be named pingpong and the port binding information for the container instance, in this case 5000→8080/tcp.

Notice that this can be an array to set more than one port binding definition.

The next annotation is @CubeDockerFile which configures how a container is created. In this case, using a Dockerfile located at default classpath location. The default location is the package+classname, so for example in previous case, Dockerfile should be placed at org/superbiz/containerobject/PingPongContainer directory.

Of course you can set any other class path location by passing as value of the annotation. The CubeDockerFile annotation sets the location where the Dockerfile is found and not the file itself.

Also this location should be reachable from ClassLoader, so it means it should be loaded from classpath in order to find it.

Any Cube can be enriched with any client side enricher, in this case with @HostIp enricher, but it could be enriched with DockerClient using @ArquillianResource as well.

Finally the @HostPort is used to translate the exposed port to bound port.

So in this example port value will be 5000. You are going to learn briefly why this annotation is important.

And then you can start using this container object in your test:

public class PingPongTest {

    PingPongContainer pingPongContainer;

    public void shouldReturnOkAsPong() throws IOException {
        String pong = ping();
        assertThat(pong, containsString("OK"));
        assertThat(pingPongContainer.getConnectionPort(), is(5000));

The most important thing here is that you need to set Container Object as a field of the class and annotate with @Cube.

It is very important to annotate the field with Cube, so before Arquillian runs the test, it can detect that it needs to start a new Cube (Docker container), create the Container Object and inject it in the test.

Notice that this annotation is exactly the same as used when you defined the Container Object.

And it is in this way because you can override any property of the Container Object from the test side. This is why @HostPort annotation is important, since port can be changed from the test definition, you need to find a way to inject the correct port inside the container object.

In this post I have introduced Container Object pattern and how can be used in Arquillian Cube. But this is only an small taste, you can read more about Arquillian Cube and Container Object integration at https://github.com/arquillian/arquillian-cube#arquillian-cube-and-container-object

Also a running examples can be found at https://github.com/arquillian/arquillian-cube/tree/master/docker/ftest-docker-containerobject

We keep learning,


The DevOps zone is brought to you in partnership with Sonatype Nexus. Use the Nexus Suite to automate your software supply chain and ensure you're using the highest quality open source components at every step of the development lifecycle. Get Nexus today


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

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

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

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

{{ parent.tldr }}

{{ parent.urlSource.name }}