Monday 28 June 2021

Junit5: Define custom condition to enable or disable tests

Step 1: Define @EnableOnJwtToken annotation. This annotation is used to execute test case when jwt token present in system property.

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@ExtendWith(EnableOnJwtTokenCondition.class)
public @interface EnableOnJwtToken {

}

 

Step 2: Define 'EnableOnJwtTokenCondition' class.

 

EnableOnJwtTokenCondition.java

package com.sample.app.conditions;

import static org.junit.jupiter.api.extension.ConditionEvaluationResult.disabled;
import static org.junit.jupiter.api.extension.ConditionEvaluationResult.enabled;
import static org.junit.platform.commons.util.AnnotationUtils.findAnnotation;

import java.util.Optional;

import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;

import com.sample.app.annotations.EnableOnJwtToken;

public class EnableOnJwtTokenCondition implements ExecutionCondition {
  private static final ConditionEvaluationResult ENABLED_BY_DEFAULT = enabled("@EnableOnJwtToken is not present");

  static final ConditionEvaluationResult ENABLED_TEST = enabled("Jwt Token present, enabled test");

  static final ConditionEvaluationResult DISABLED_TEST = disabled("Jwt token not present, disabled test");

  @Override
  public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
    Optional<EnableOnJwtToken> optional = findAnnotation(context.getElement(), EnableOnJwtToken.class);
    if (optional.isPresent()) {

      String token = System.getProperty("jwtToken");

      if (token != null) {
        return ENABLED_TEST;
      } else {
        return DISABLED_TEST;
      }

    }
    return ENABLED_BY_DEFAULT;
  }

}

Step 3: Use ‘@EnableOnJwtToken’ annotation on test methods.

 

CustomConditionTest.java

package com.sample.app;

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

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

import com.sample.app.annotations.EnableOnJwtToken;

public class CustomConditionTest {

  @BeforeAll
  static void setupJWTToken() {
    System.setProperty("jwtToken", "token123");
  }

  @Test
  @EnableOnJwtToken
  void test1() {
    System.out.println("Executing test1");
    assertTrue(true);
  }

  @Test
  @EnableOnJwtToken
  void test2() {
    System.out.println("Executing test2");
    assertTrue(true);
  }

}


Run above test class, you can observe both the test cases gets executed.




Suppose, if you do not set below system property, then test cases skipped.

System.setProperty("jwtToken", "token123");

 

CustomConditionTest1.java

package com.sample.app;

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

import org.junit.jupiter.api.Test;

import com.sample.app.annotations.EnableOnJwtToken;

public class CustomConditionTest1 {
  @Test
  @EnableOnJwtToken
  void test1() {
    System.out.println("Executing test1");
    assertTrue(true);
  }

  @Test
  @EnableOnJwtToken
  void test2() {
    System.out.println("Executing test2");
    assertTrue(true);
  }
}

 

Run CustomConditionTest1 class, you will observe that the test cases skipped.

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment