Junit 5 @Tag Meta Annotation.

A JUnit5 test case may fall under may scenario, the same test case might be eligible for multiple scenarios, we can annotate the Junit5 test case with multiple @Tag annotations as required.
At the run time, only those test cases will run which satisfy all @Tag conditions in the Gradle test task.

package org.wesome.junit5;

public class AppleCalculator {
    public int addApple(int apple1, int apple2) {
        return apple1 + apple2;
    }

    public int subApple(int apple1, int apple2) {
        return apple1 - apple2;
    }
}
package org.wesome.junit5;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class AppleCalculatorTest {
    @Test
    @Tag("add")
    @Tag("positive")
    void addAppleTestCase1() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }

    @Test
    @Tag("add")
    @Tag("negative")
    void addAppleTestCase2() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(-2, appleCalculator.addApple(-1, -1), "-1 apple +  -1 apple is 2 apple");
    }

    @Test
    @Tag("sub")
    @Tag("positive")
    void subAppleTestCase1() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(1, appleCalculator.subApple(2, 1), "2 apple - 1 apple is 1 apple");
    }

    @Test
    @Tag("sub")
    @Tag("negative")
    void subAppleTestCase2() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(-1, appleCalculator.subApple(-2, -1), "-2 apple - 1 apple is -1 apple");
    }
}
plugins {
    id 'java'
    id "io.freefair.lombok" version "6.2.0"
}

group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8

repositories {
    mavenCentral()
}
dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

test {
    testLogging {
        events "started", "passed", "skipped", "failed"
    }
    useJUnitPlatform {
        includeTags 'add'
        excludeTags 'sub'
    }
}

If we are using the same tag multiple times then the better way is to create a custom tag.

package org.wesome.dsalgo;

public class AppleCalculator {
    public int addApple(int apple1, int apple2) {
        return apple1 + apple2;
    }

    public int subApple(int apple1, int apple2) {
        return apple1 - apple2;
    }
}
package org.wesome.dsalgo;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class AppleCalculatorTest {
    @Test
    @AddPositive
    void addPositiveAppleTestCase() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }

    @Test
    @AddNegative
    void addNegativeAppleTestCase() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(-2, appleCalculator.addApple(-1, -1), "-1 apple +  -1 apple is 2 apple");
    }

    @Test
    @SubPositive
    void subPositiveAppleTestCase() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(1, appleCalculator.subApple(2, 1), "2 apple - 1 apple is 1 apple");
    }

    @Test
    @SubNegative
    void subNegativeAppleTestCase() {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(-1, appleCalculator.subApple(-2, -1), "-2 apple - 1 apple is -1 apple");
    }
}
package org.wesome.dsalgo;

import org.junit.jupiter.api.Tag;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Retention(RUNTIME)
@Target({TYPE, METHOD})
@Tag("add")
@Tag("negative")
@interface AddNegative {
}

@Retention(RUNTIME)
@Target({TYPE, METHOD})
@Tag("add")
@Tag("positive")
@interface AddPositive {
}

@Retention(RUNTIME)
@Target({TYPE, METHOD})
@Tag("sub")
@Tag("negative")
@interface SubNegative {
}

@Retention(RUNTIME)
@Target({TYPE, METHOD})
@Tag("sub")
@Tag("positive")
@interface SubPositive {
}
plugins {
    id 'java'
    id "io.freefair.lombok" version "6.2.0"
}

group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8

repositories {
    mavenCentral()
}
dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

test {
    testLogging {
        events "started", "passed", "skipped", "failed"
    }
    useJUnitPlatform {
        includeTags 'add', 'positive'
        excludeTags 'sub', 'positive'
    }
}

follow us on