Testing

Testing is a crucial part of application development and should not be overlooked. SeedStack can help you to test your application by providing the necessary tools.

Unit testing

SeedStack doesn’t provide specific tools for Unit-testing.

It only provides useful testing libraries like JUnit 4, AssertJ and Mockito.

Integration testing

You can do integration testing on non-Web parts of the application by making your test class extend the AbstractSeedIT class.

Add a GreeterServiceIT in the org.generated.project.domain.services under the src/test/java folder:

package org.generated.project.domain.services;

import static org.assertj.core.api.Assertions.assertThat;

import javax.inject.Inject;
import org.generated.project.domain.model.person.Person;
import org.generated.project.domain.model.person.PersonId;
import org.junit.Test;
import org.seedstack.seed.it.AbstractSeedIT;

public class GreeterServiceIT extends AbstractSeedIT {
    @Inject
    private GreeterService greeterService;

    @Test
    public void testGreeting() throws Exception {
        Person person = new Person(new PersonId("test@som.org"));
        person.changeName("testFirstName", "testLastName");
        assertThat(greeterService.greet(person)).isEqualTo("Hello testFirstName testLastName!");
    }
}

The runner starts SeedStack before executing test methods and then shutdown SeedStack properly.

All SeedStack features work as usual during the test. The test class itself can be injected and be the target of AOP interception. For instance a test method can be made transactional.

Web integration testing

If you already explored the project structure, you have probably found the HelloResourceIT class in the test part of the application (src/test/java).

This is a Web integration test that uses Arquillian to execute the Web application and validate some assertions on its REST API.

In SeedStack, by default, Arquillian is configured to use an embedded version of Tomcat on port 9090.

Let’s update the HelloResourceIT class to correspond to the current state of our application:

package org.generated.project.interfaces.rest;

import io.restassured.response.Response;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import org.seedstack.seed.it.AbstractSeedWebIT;

import java.net.URL;

import static io.restassured.RestAssured.given;
import static org.assertj.core.api.Assertions.assertThat;

public class HelloResourceIT extends AbstractSeedWebIT {
    @ArquillianResource
    private URL baseURL;

    @Deployment
    public static WebArchive createDeployment() {
        return ShrinkWrap.create(WebArchive.class);
    }

    @Test
    @RunAsClient
    public void testHelloWorld() throws Exception {
        Response response = given()
                .auth().basic("demo", "demo")
                .expect().statusCode(200)
                .when().get(baseURL + "hello");

        assertThat(response.body().asString()).isEqualTo("Hello Ella FITZGERALD!");
    }
}

Tested WebApp

The test class extends AbstractSeedWebIT which has the effect of running the test with Arquillian.

The Web archive (WAR) is programmatically created in the createDeployment() static method. It can be customized by adding classes, resources and metadata using the ShrinkWrap API. This is handy for testing alternatives of the application.

The base URL of the application (which is randomized) is injected by Arquillian using the @ArquillianResource annotation.

Test method

The test method is annotated by @RunAsClient which make it run in a separate thread that can invoke the WebApp from the outside.

The test method body uses RestAssured and AssertJ to do the testing job.

The test request contains basic authentication credentials, which can be of use if you uncomment the security configuration in the application.yaml file.

Now what ?

What we learned

In this page you have learned:

  • That you can write Unit tests as usual without any SeedStack specificity.
  • How to write a standard (non-Web) integration test.
  • How to write a Web integration test.

Troubleshooting

If you can’t get this to work, check the troubleshooting page.

Missing accomplished!

We are done with the tutorial. Congratulations for getting this far!

You can now go further and explore the samples or start to read the reference documentation

   

On this page


Edit