HOW-TO TwiP

You can download TwiP from sourceforge (see link on the left) and add it to your classpath. If you use Maven, you can instead add a dependency to your pom:

<dependency>
  <groupId>net.sf.twip</groupId>
  <artifactId>twip</artifactId>
  <version>3.0</version>
  <scope>test</scope>
</dependency>

TwiP requires JUnit 4.5 and Java 1.5.

To use TwiP, annotate your JUnit test class with @RunWith(TwiP.class). After that you can just add parameters to your test methods... that's it. If you for example add a primitive boolean parameter, your test method is called once with true and once with false. If you use a Boolean object parameter instead, your test will additionally be called with null.

@Test
void myTest(boolean flag1) {
    ...
}

The same thing works for all other primitive types and their object counterparts, as well as for enums and strings. As mentioned earlier it is not reasonably possible to call a method with every single value in the complete range of e.g. a long, not to mention all possible string values! Instead a subset of commonly failing values is used by default, e.g. 1, 1.5, 2, 2.5, 3, 0, 1.0 / 9, -1, -1.5, -2, -2.5, -3, 127, -127, 1024, -1024, NaN, NEGATIVE_INFINITY, POSITIVE_INFINITY, MAX_VALUE, and MIN_VALUE for floats.

But you may need to call your tests with other than the default set of values or with other types. Simply define a public static field or method and annotate your test parameter with @Values and a string argument with the name of the field of method. For example:

public static final String[] MY_STRINGS = { "A", "B", "C" };

@Test
public void myTest(@Values("MY_STRINGS") String string) {
    ...
}

Note that the type of such a constant or the return type of such a method must be an array or a collection of the matching type.

If you have an enum or String argument, then null is by default one possible value. If you don't want to test null, annotate the argument with @NotNull. (Primitive types can't be null, so you can use e.g. long instead of Long for the same purpose).

@Test
public void myTest(@NotNull String string) {
    ...
}

Sometimes your code can't handle some of the values passed in by default. E.g. you may want to test your division method, so the second parameter mustn't be zero. Use an @Assume annotation with an expression argument like this:

@Test
public void testDivision(long a, @Assume("!= 0") b) {
    ...
}

Note that the assumption expression language is very limited. You can only compare numbers with >, >=, !=, <=, and < to a number, and != NaN, != INF, and != -INF for floats and doubles. Note that null "slips through" these tests, i.e. if you assume that an Integer argument is >= 0, then null is still passed in. You can chain several expressions with & as a logical AND.

Often you want the same set of values for all test methods in one class. If the type of the parameter is unique, it's sufficient to annotate the public static field or method with @AutoTwip and the values will get injected automatically.

@AutoTwip
public static final MyType[] MY_TYPES = { new MyType(), new MyType(), new MyType() };

@Test
public void myTest(MyType myType) {
    ...
}

Note that the type of an AutoTwip constant or the return type of an AutoTwip method must be an array or a collection of a type that can be assigned to the parameter. Only AutoTwip methods can also return a single instance. And all AutoTwip methods can take parameters themselves:

public static class Person {
    private short age;
    private String name;
    ...
}

@AutoTwip
public static Person persons(short age, String name) {
        return new Person(age, name);
};

@Test
public void myTest(Person person) {
    ...
}