Code Monkey home page Code Monkey logo

testnj's Introduction

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());
    }

}

testnj's People

Contributors

giulio avatar ssprang avatar

Watchers

 avatar Derek Reed avatar Magnus Aspling avatar  avatar  avatar  avatar Anders Weijnitz avatar James Cloos avatar Andreas Nilsson avatar mnova avatar Oscar Norlander avatar  avatar Mark avatar Mattias Johansson avatar  avatar wkuo avatar  avatar  avatar Tobbe Brolin avatar  avatar  avatar  avatar Anders Weijnitz avatar  avatar Petri Ikonen avatar Pete Rabjohns avatar Osamu Ishiyama avatar Manuel Stadelmann avatar  avatar  avatar  avatar  avatar  avatar  avatar Eng Cheng Kait avatar  avatar  avatar  avatar  avatar Gianluca Orlando avatar  avatar  avatar Nur Aini Abd Wahab avatar Johan Lekberg avatar  avatar Max Negro avatar Daniele Autizi avatar  avatar Don Lind avatar Daniel Lind avatar  avatar  avatar Jonas Romfelt avatar Martin Hedenberg avatar  avatar  avatar Keith Judson avatar  avatar  avatar  avatar  avatar  avatar  avatar ugopat avatar

Forkers

uoloyola

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.