junit-runif

Run unit test conditionally.

License

License

Categories

Categories

JUnit Unit Testing
GroupId

GroupId

com.github.mjeanroy
ArtifactId

ArtifactId

junit-runif
Last Version

Last Version

0.2.0
Release Date

Release Date

Type

Type

jar
Description

Description

junit-runif
Run unit test conditionally.
Project URL

Project URL

https://github.com/mjeanroy/junit-servers
Source Code Management

Source Code Management

https://github.com/mjeanroy/junit-runif

Download junit-runif

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.mjeanroy/junit-runif/ -->
<dependency>
    <groupId>com.github.mjeanroy</groupId>
    <artifactId>junit-runif</artifactId>
    <version>0.2.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.mjeanroy/junit-runif/
implementation 'com.github.mjeanroy:junit-runif:0.2.0'
// https://jarcasting.com/artifacts/com.github.mjeanroy/junit-runif/
implementation ("com.github.mjeanroy:junit-runif:0.2.0")
'com.github.mjeanroy:junit-runif:jar:0.2.0'
<dependency org="com.github.mjeanroy" name="junit-runif" rev="0.2.0">
  <artifact name="junit-runif" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.mjeanroy', module='junit-runif', version='0.2.0')
)
libraryDependencies += "com.github.mjeanroy" % "junit-runif" % "0.2.0"
[com.github.mjeanroy/junit-runif "0.2.0"]

Dependencies

provided (1)

Group / Artifact Type Version
junit : junit jar 4.12

test (4)

Group / Artifact Type Version
org.assertj : assertj-core jar 2.9.0
org.mockito : mockito-core jar 2.18.0
org.apache.commons : commons-lang3 jar 3.7
nl.jqno.equalsverifier : equalsverifier jar 2.4.5

Project Modules

There are no modules declared in this project.

junit-runif

Build Status

Introduction

Run your test conditionally!

Why?

On some project, I need to run the entire test suite at least on the JDK 7 and JDK 8. But if some dependencies require JDK 8, you need to ignore some unit tests conditionally, that is why I wrote this library.

Installation

Maven

<dependency>
  <groupId>com.github.mjeanroy</groupId>
  <artifactId>junit-runif</artifactId>
  <version>0.1.0</version>
  <scope>test</scope>
</dependency>

How to use

Run your test with RunIfRunner and add RunIf annotation where you need!

For example, here a unit test that will run only on Windows:

import org.junit.Test;
import org.junit.runner.RunWith;

import com.github.mjeanroy.junit4.runif.RunIf;
import com.github.mjeanroy.junit4.runif.RunIfRunner;

@RunWith(RunIfRunner.class)
public class MyUnitTest {
    @Test
    @RunIf(WindowdCondition.class)
    public void it_should_run_on_windows_only() {
        // Do your test
    }
}

Here is the condition implementation:

import com.github.mjeanroy.junit4.runif.RunIfCondition;

public class WindowsCondition implements RunIfCondition {
    @Override
    public boolean apply() {
        return System.getProperty("os.name").toLowerCase().indexOf("win") >= 0;
    }
}

Why a runner and not a junit Rule?

This library use a custom JUnit rule since using a Runner will allow you to not load a class if it is ignored. This is especially important if you want to run unit test that use JDK 8 API but you execute your build with a JDK7.

For example:

import org.junit.Test;
import org.junit.runner.RunWith;

import com.github.mjeanroy.junit4.runif.RunIf;
import com.github.mjeanroy.junit4.runif.RunIfRunner;
import com.github.mjeanroy.junit4.runif.conditions.AtLeastJava8Condition;

@RunWith(RunIfRunner.class)
@RunIf(AtLeastJava8Condition.class)
public class MyUnitTest {
    @Test
    public void it_should_run_on_windows_only() {
        // Do your test with JDK 8 API
    }
}

It is really important to add the RunIf annotation on the class and not on the method, otherwise the class will have to be loaded and your test will fail on JDK 7.

Available Conditions

Out of the box, following conditions are available:

  • Java7Condition: ensure that the (runtime) java version is exactly java 7.
  • Java8Condition: ensure that the (runtime) java version is exactly java 8.
  • AtLeastJava7Condition: ensure that the (runtime) java version is greater than or equal to java 7.
  • AtLeastJava8Condition: ensure that the (runtime) java version is greater than or equal to java 8.

License

MIT License.

Contributing

If you found a bug or you thing something is missing, feel free to contribute and submit an issue or a pull request.

Versions

Version
0.2.0
0.1.0