Skip to content
Pierre T. edited this page Apr 19, 2015 · 4 revisions

Dependency

To use the testing features provided by Nuun add the following dependency to your pom.

<dependency>
    <groupId>io.nuun</groupId>
    <artifactId>kernel-tests</artifactId>
    <version>${nuunkernel.version}</version>
    <scope>test</scope>
</dependency>

Usage

JUnit runner

Nuun provides a JUnit runner NuunITRunner for integration testing. This runner starts a Nuun kernel and bind the test class. This means that you can inject classes bound by Nuun in your test class.

Nuun IT also provides a @WithParams annotation which allows to pass params to the kernel. For instance, in the above example, we specify the Kernel.NUUN_ROOT_PACKAGE which indicates to the kernel the root package to scan. This can be very useful if you want to test different topology of bindings.

@WithParams({Kernel.NUUN_ROOT_PACKAGE, "org.mycompany.myproject"})
@RunWith(NuunITRunner.class)
public class ServiceAIT {
    
    @Inject
    ServiceA serviceA;
    
    @Test
    public void serviceAShouldDoSomethingUseful() {
        Assertions.assertThat(serviceA).isNotNull();
        serviceA.doSomethingUseful();
        ...
    }
}

Nuun also provides an @Expect annotation which allows to specify an expected exception. It can be used to test error cases.

For instance, if we want to test that we can't inject a service without using its interface, we will do it as follows.

@Expect(ConfigurationException.class)
@WithParams({Kernel.NUUN_ROOT_PACKAGE, "org.mycompany.myproject2"})
@RunWith(NuunITRunner.class)
public class ServiceAIT {
    
    @Inject
    ServiceAImpl serviceA;
    
    @Test
    public void implementationsShouldNotBeInjectable() {
        Assertions.assertThat(serviceA).isNull();
    }
}

Notice that the root package is different in the two tests. This allows to isolate test cases and avoid the error case to break the other tests.

Bindings

The test class is automatically bind by you may want to bind other classes for your testing needs. Nuun provides two API for that:

  • The @ITBind annotation which can be used on a class to bind it
  • The @KernelModule annotation which is used to bind native module without having to create a plugin. This feature is only available for test purpose.

Content

Clone this wiki locally