@Tag Meta Annotation allows us to combine multiple, we can create custom @Tag Specific Gradle Task to run the required scenario of the test case.
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.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'
}
}
tasks.register("add", Test) {
testLogging {
events "started", "passed", "skipped", "failed"
}
useJUnitPlatform {
includeTags 'add'
}
mustRunAfter check
}
gradle clean add
or
./gradlew clean add