/junit-easy-tools

JUnit 4 extensions

Primary LanguageJavaMIT LicenseMIT

Download Build Status codecov Dependency Status Codacy Badge

junit-easy-tools

Extensions for JUnit4

@DataProducer

Provides lazy calculated parameters inserted to test method.

Example

Simple example

The value of stringProducer will be calculated and injected to the testMethod(String string)

@DataProducer
public static Supplier<String> stringProducer = () -> RandomStringProvider.get();

@Test
public void testMethod(String string) {
   ...
}

Example with iterations

The testMethod(String string) will run three times (@ProducedValues(iterations = 3)). Each run the value of stringProducer will be recalculated, so the new value will be provided.

@DataProducer
public static Supplier<String> stringProducer = () -> RandomStringProvider.get();

@Test
@ProducedValues(iterations = 3)
public void testMethod(String string) {
   ...
}

Named @DataProducer

To testMethod(@ProducedValue(producer = "calculated") String string) will be injected values from calculatedProducer. If no name is specified for @DataProducer, the field name will be considered as @DataProducer name.

@DataProducer
public static Supplier<String> randomProducer = () -> RandomStringProvider.get();

@DataProducer(name = "calculated")
public static Supplier<String> calculatedProducer = () -> CalculatedStringProvider.get();

@Test
public void testMethod(@ProducedValue(producer = "calculated") String string) {
   ...
}

Multiple @DataProducer of the same type

Method test(String s) will be executed twice. The first time value from random @DataProducer will be injected. The second time value from queue @DataProducer will be injected.

@DataProducer
public static Supplier<String> random = () -> RandomStringProvider.get();

@DataProducer
public static Supplier<String> queue = () -> StringQueue.next();
 
@Test
public void test(String s) {
    ...
}

Multiple @DataProducer of the same type and iterations

Method test(String s) will be executed six times. Three iterations with each @DataProducer.

@DataProducer
public static Supplier<String> random = () -> RandomStringProvider.get();

@DataProducer
public static Supplier<String> queue = () -> StringQueue.next();

@Test
@ProducedValues(iterations = 3)
public void test(String s) {
    ...
}