Skip to content

Latest commit

 

History

25 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

Test NJ

Framework for JUnit test injection and lifecycle hooks based on JUnit 4 and Guice.

Getting Started

1. Add a Maven dependency to your project

<dependency>
  <groupId>com.polopoly</groupId>
  <artifactId>testNJ</artifactId>
  <version>${testNJ.version}</version>
</dependency>

2. Bindings

Craete a Guice module for your tests:

package com.myproject;

import com.google.inject.AbstractModule;

public class Module extends AbstractModule {

    @Override
    protected void configure() {
        bind(Duck.class).toInstance(new SwedishBlueDuck());
    }

}

TestNJ uses the Java Service Locator pattern to look for implementations of Guice modules in your projects (implementations of com.google.inject.Module).

Add a file named com.google.inject.Module in your project's META-INF/services folder, this file should contain the names of the Guice module implementation classes, in our example:

echo 'com.project.Module' > ${prj.root}/META-INF/services/com.google.inject.Module

3. Write your test

@RunWith(TestNJRunner.class)
public class SimpleInjectTest {

    @Inject
    private Duck duck;

    @Test
    public void duck_does_quack() {
        Assert.assertEquals("haloj", duck.quack());
    }

}

4. Use Callbacks

You can register test callbacks and have your custom code run before/after a single test run.

Simply implement your callback:

package com.myproject;

import com.polopoly.testnj.TestCallbacks;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;

public class DummyTestCallbacks implements TestCallbacks {

    @Override
    public void before(FrameworkMethod method, Object target, Statement statement) {
        System.err.println("before");
    }

    @Override
    public void after(FrameworkMethod method, RunNotifier notifier) {
        System.err.println("after");
    }

}

... and register it in your Guice Module:

package com.myproject;

import com.google.inject.AbstractModule;

public class Module extends AbstractModule {

    @Override
    protected void configure() {
        bind(Duck.class).toInstance(new SwedishBlueDuck());
        TestNJContext.addCallback(new DummyTestCallbacks());
    }

}

About

framework for JUnit test injection and lifecycle hooks

Resources

Stars

0 stars

Watchers

62 watching

Forks

Releases

Packages

Contributors

Languages