Test Lifecycle

Jitr uses JUnit 4's @RunWith annotation to hook into JUnit's test lifecycle. Annotating your test class with @RunWith<Jitr.class> will let Jitr manage the container while you focus on the testing. Here's the order in which things happen:

step component
in charge
test phase activity
 1 Jitr continer startup Jitr starts the container
 2 JUnit before class JUnit calls your static methods annotated with @BeforeClass
 3 Jitr Spring autowiring Jitr uses Spring to autowire your test class instance
 4 Jitr field setting Jitr sets any fields annotated with Jitr annotations
*5 JUnit before test JUnit calls your methods annotated with @Before
*6 JUnit test JUnit runs your test method
*7 JUnit after test JUnit calls your methods annotated with @After
 8 JUnit after class JUnit calls your static methods annotated with @AfterClass
 9 Jitr container shutdown Jitr shuts down the container

As is always the case with JUnit tests, steps 5 - 7 repeat for every test method in your class.

Example

The following provides a very simple test showing you the basics of Jitr. In this example, we will use the default Jitr configuration and the same Spring application context as the web application. Since we have the same bean instances as the web application, we can do any initialization we would like before our integration tests start.

To enhance the example, we have also set a custom context path. You can see in the test that the full base URI that is built is: http://localhost:{port}/example . This is the format for all base URIs.

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.jitr.annotation.BaseUri;
import org.jitr.annotation.JitrConfiguration;
import org.jitr.annotation.Port;
import org.jitr.test.service.TestService;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;

@RunWith(Jitr.class)
@JitrConfiguration(contextPath = "/example")
public class ExampleTest {

    private static final String TEST_STRING = "TEST_STRING";

    @Port
    private int port;

    @BaseUri
    private String baseUri;

    @Autowired
    private TestService testService;

    private HttpClient httpClient;

    private HttpMethod method;

    @After
    public void after() {
        if (method != null) {
            method.releaseConnection();
        }
    }

    @Before
    public void before() {

        httpClient = new HttpClient();

        // make sure the Spring autowiring was successful
        Assert.assertNotNull(testService);

        // make sure the Jitr fields have been set properly
        Assert.assertNotSame(-1, port);
        Assert.assertNotSame(8080, port);

        Assert.assertNotNull(baseUri);
        Assert.assertTrue(baseUri.length() > 0);
        Assert.assertTrue(baseUri.contains("http://localhost:" + port + "/example"));
    }

    @Test
    public void testServerGet() throws Exception {

        // set value on bean
        testService.setString(TEST_STRING);

        // make sure value was set on bean properly
        Assert.assertEquals(TEST_STRING, testService.getString());

        // fetch the same value from server through HTTP GET
        method = new GetMethod(baseUri + "/string");
        httpClient.executeMethod(method);

        // make sure the value from the server is the same
        Assert.assertEquals(TEST_STRING, method.getResponseBodyAsString());
    }
}