1.2.1 (Apr 3, 2016)
Aug 30, 2015
Nov 5, 2018 (Retired)
Vladislav Bauer (vbauer)
Vladislav Bauer (vbauer)
Source code



There is an exception to every rule.

JConditions is an extension for JUnit framework, which allows to mark test methods with specific conditional annotations. This annotations allow to permit or restrict execution of test methods. It helps to keep clean your test methods using declarative programming and prevents a lot of unnecessary code (see Assume class).

10 second example

A picture paints a thousand words:

public class ExampleTest {
    public void testRunningOnOS() throws Exception {
        // Check some Linux app
    public void testIfJavaVersion8() {
        // Check some Java 8 specific code


JConditions has the following conditional annotations:

This annotations could be used with test methods or/and classes (it will allow to run checks before each test method). You can also write custom annotations or make composite annotations.


JConditions uses JitPack.io for distribution, so you need to configure JitPack's Maven repository to fetch artifacts (dependencies).





repositories {
    maven {
        url 'https://jitpack.io'

dependencies {
    testCompile 'com.github.vbauer:jconditions:1.2.3'


Some things that you need to know before exploring an examples:

  • Each described annotation has a simple example how to use it. Examples are very simple, they are existed only to show the main idea of usage.
  • Most of them could use injections to insert environment variables. This injections/substitutions could be useful to parametrize tests or to organize profiles. EX: "${java.io.tmpdir}/test.html"


@AppIsInstalled checks that specified application(s) is installed. It could be useful when developed application has an optional integrations with some external tools/apps and it will be good to check this integrations in tests.

@AppIsInstalled({ "ls", "uname" })
public void testAppIsInstalled() throws Exception {


@ExistsOnFS checks that specified file or directory is existed on file system. It is also possible to configure multiple values.

Available parameters:

  • value - file(s) or directory(ies).
  • type - type(s) of FS element (FILE / DIRECTORY / SYMLINK).
public void testFileExists() throws Exception {


@HasClass checks that specified class(es) is available in classpath. It could be useful to check integration with optional features.

public void testHasClass() throws Exception {


@HasFreeSpace checks that FS element(s) / disk(s) has free space. It could be useful to check if it is possible to:

  • download some big some file from remote server
  • generate and store on FS some data

Available parameters:

  • value - disk or disks that should be checked (ex: "C:\").
  • min - minimum amount of available free space on disk in bytes.
  • max - maximum amount of available free space on disk in bytes.
@HasFreeSpace(value = { "/", "C:\\" }, min = 1024)
public void testHasFreeSpace() {
    // Download some file


@HasPackage checks that specified package(s) is available in classpath. It could be useful to check integration with optional features.

public void testHasPackage() throws Exception {


@IfJavaVersion checks that test is run on the specific version(s) of JVM.

EX: Some features could use external libraries and work only for the specific version of JVM. It is possible to check it, if you use full names for classes (class loader will load them in runtime).

public void testIfJavaVersion8() {
    // Javaslang project works only on Java 8


@IfScript allows to write custom conditional rules using JSR 223: Scripting for the JavaTM Platform. JavaScript engine is available by default (it is part of JVM). All other JSR233-compatible languages will be included automatically if they are available in classpath.

Available parameters:

  • values - script or scripts that should be executed. Return value will be converted to boolean type (even String and Numbers).
  • engine - type of script engine (default value is "js").
  • context - context provider which provides an extra data in script as "context" variable.

Parameters which are available in script context:

  • test - current instance of running test class.
  • env - environment variables (System.getenv()).
  • props - system properties (System.getProperties()).
  • console - console object (System.console()).
  • context - extra data which could be created using context provider.
public void testIfScriptNegative() {


@IgnoreIf allows to skip some test method using specific ConditionChecker class. It will skip test, if checker return true and execute method otherwise. ConditionChecker could be separate class, or nested static class, or even inner class. It also works fine with private classes.

public void testIgnoreIfAlways() {


@PropertyIsDefined checks if environment or system property is defined. It could be useful to simulate test profiles or to check some cases which are dependent on them (and could be optionals).

Available parameters:

  • keys - keys of environment or system variables
  • values - values that should correspond to keys (optional parameter).
@PropertyIsDefined(keys = "os.name")
public void testEnvVarIsDefined() {


@ResourceIsAvailable allows to minimize code which is necessary to download some document/file via HTTP/HTTPS. It is also possible to cache downloaded resource between test executions, otherwise it will be remove after test.

Available parameters:

  • source - HTTP/HTTPS file or document.
  • target - path to file where content should be saved.
  • cache - flag to configure cache option.
  • timeout - maximum time for connection to the source (default value is 10sec).
    source = "http://apple.com",
    target = "${java.io.tmpdir}/apple-homepage.html",
    cache = false
public void testResourceIsAvailable() {
    final String path = PropUtils.injectProperties("${java.io.tmpdir}/apple-homepage.html");


@RunIf is an opposite annotation to @IgnoreIf. It will run test method if ConditionChecker returns true.

public void testInnerClass() {


@RunningOnOS checks the current operation system and runs test method only when it is specified and value parameter. It is also possible to configure multiple variants (to run test method, even one of them should be fine).

public void testRunningOnOS() throws Exception {


@SocketIsOpened checks that specified socket is opened.

Available parameters:

  • host - host address (default value is "").
  • port - socket's port number.
  • timeout - maximum time for connection to the socket (default value is 10sec).
@SocketIsOpened(host = "apple.com", port = 80)
public void testSocketIsOpened() throws Exception {


@UrlIsReachable checks that specified URL address is reachable (available via URLConnection). It also possible to configure multiple URLs.

Available parameters:

  • value - URL address(s) that should be checked.
  • timeout - maximum timeout for URL connection (default value is 10sec).
public void testUrlIsReachable() throws Exception {

Custom annotations

It is possible use @IfRun or @IgnoreIf to run custom ConditionalChecker, but it is also possible to write your own annotation (like @HasPackage or @IfScript).

All JConditions out-of-box annotations was created using unified extension mechanism. This mechanism is centered around 3 main things:

  • Custom annotation which can has additional parameters that could be used as input data.
  • Conditional checker which make decision to permit or restrict running of test method.
  • @Condition annotation which allows to glue custom annotation and conditional checker.

Let's write an annotation which emulates standard JUnit's @org.junit.Ignore:

@Target({ ElementType.ANNOTATION_TYPE, ElementType.TYPE, ElementType.METHOD })
public @interface IgnoreIt {

public class IgnoreItChecker implements ConditionChecker<IgnoreIt> {
    public boolean isSatisfied(final CheckerContext<IgnoreIt> context) {
        return false;


That's all! Now, you can mark test classes or test methods with @IgnoreIt to skip test(s).

Composite annotations

Sometimes it could be useful to have possibility to resolve the following cases (to prevent unnecessary code):

  • Specify all needed parameter for some existed annotation and do not copy-paste them.
  • Glue some conditional annotations into one annotation.

It is possible, because JConditions extension mechanism resolves all hierarchy of classes and annotations.

Let's make an annotation which allows to detect if our MySQL database works fine:

@SocketIsOpened(host = "localhost", port = 3306)
@Target({ ElementType.ANNOTATION_TYPE, ElementType.METHOD })
public @interface MySQLWorks {

Let's make an annotation which allows to run tests only on Linux machines with Java 8:

@Target({ ElementType.ANNOTATION_TYPE, ElementType.METHOD })
public @interface OnLinuxWithJava8 {

Building from source

JConditions uses Maven for its build. To build project, run:

mvn clean install -P strict

from the root of the project directory. Profile strict is necessary to check code style and to run static code analysis.

Might also like

  • jackdaw - Java Annotation Processor which allows to simplify development.
  • caesar - Library that allows to create async beans from sync beans.
  • houdini - Type conversion system for Spring framework.
  • herald - Logging annotation for Spring framework.
  • commons-vfs2-cifs - SMB/CIFS provider for Commons VFS.
  • avconv4java - Java interface to avconv tool.


Copyright 2015 Vladislav Bauer

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at


Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

See LICENSE file for details.