org.junit.ClassRule Java Examples

The following examples show how to use org.junit.ClassRule. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SpringMethodRule.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Throw an {@link IllegalStateException} if the supplied {@code testClass}
 * does not declare a {@code public static final SpringClassRule} field
 * that is annotated with {@code @ClassRule}.
 */
private static SpringClassRule validateSpringClassRuleConfiguration(Class<?> testClass) {
	Field ruleField = null;

	for (Field field : testClass.getFields()) {
		if (ReflectionUtils.isPublicStaticFinal(field) && SpringClassRule.class.isAssignableFrom(field.getType())) {
			ruleField = field;
			break;
		}
	}

	if (ruleField == null) {
		throw new IllegalStateException(String.format(
				"Failed to find 'public static final SpringClassRule' field in test class [%s]. " +
				"Consult the javadoc for SpringClassRule for details.", testClass.getName()));
	}

	if (!ruleField.isAnnotationPresent(ClassRule.class)) {
		throw new IllegalStateException(String.format(
				"SpringClassRule field [%s] must be annotated with JUnit's @ClassRule annotation. " +
				"Consult the javadoc for SpringClassRule for details.", ruleField));
	}

	return (SpringClassRule) ReflectionUtils.getField(ruleField, null);
}
 
Example #2
Source File: HBaseClassTestRuleChecker.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Override
public void testStarted(Description description) throws Exception {
  Category[] categories = description.getTestClass().getAnnotationsByType(Category.class);

  // @Category is not repeatable -- it is only possible to get an array of length zero or one.
  if (categories.length == 1) {
    for (Class<?> c : categories[0].value()) {
      if (c == IntegrationTests.class) {
        return;
      }
    }
  }
  for (Field field : description.getTestClass().getFields()) {
    if (Modifier.isStatic(field.getModifiers()) && field.getType() == HBaseClassTestRule.class &&
      field.isAnnotationPresent(ClassRule.class)) {
      HBaseClassTestRule timeout = (HBaseClassTestRule) field.get(null);
      assertEquals(
        "The HBaseClassTestRule ClassRule in " + description.getTestClass().getName() +
          " is for " + timeout.getClazz().getName(),
        description.getTestClass(), timeout.getClazz());
      return;
    }
  }
  fail("No HBaseClassTestRule ClassRule for " + description.getTestClass().getName());
}
 
Example #3
Source File: ConsulRegistrationTest.java    From grpc-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
@ClassRule
public static final ConsulResource consul(){
    int port = SocketUtils.findAvailableTcpPort();
    ConsulResource consulResource = new ConsulResource(port);
    System.setProperty("spring.cloud.consul.port",String.valueOf(port));
    return consulResource;
}
 
Example #4
Source File: TestCoverageProcessEngineRule.java    From camunda-bpm-process-test-coverage with Apache License 2.0 5 votes vote down vote up
/**
 * Validates the annotation of the rule field in the test class.
 * 
 * @param description
 */
private void validateRuleAnnotations(Description description) {

    // If the first run is a @ClassRule run, check if @Rule is annotated
    if (firstRun && !description.isTest()) {

        /*
         * Get the fields of the test class and check if there is only one
         * coverage rule and if the coverage rule field is annotation with
         * both @ClassRule and @Rule.
         */

        int numberOfCoverageRules = 0;
        for (Field field : description.getTestClass().getFields()) {

            final Class<?> fieldType = field.getType();
            if (getClass().isAssignableFrom(fieldType)) {

                ++numberOfCoverageRules;

                final boolean isClassRule = field.isAnnotationPresent(ClassRule.class);
                final boolean isRule = field.isAnnotationPresent(Rule.class);
                if (isClassRule && !isRule) {

                    throw new RuntimeException(getClass().getCanonicalName()
                            + " can only be used as a @ClassRule if it is also a @Rule!");
                }
            }
        }

        // TODO if they really want to have multiple runs, let them?
        if (numberOfCoverageRules > 1) {
            throw new RuntimeException("Only one coverage rule can be used per test class!");
        }
    }
}
 
Example #5
Source File: LoadTimeWeavableTestRunner.java    From rice with Educational Community License v2.0 5 votes vote down vote up
/**
 * @return the {@code ClassRule}s that can transform the block that runs
 *         each method in the tested class.
 */
protected List<TestRule> classRules() {
    List<TestRule> result = getTestClass().getAnnotatedMethodValues(null, ClassRule.class, TestRule.class);

    result.addAll(getTestClass().getAnnotatedFieldValues(null, ClassRule.class, TestRule.class));

    return result;
}
 
Example #6
Source File: RuleContext.java    From spectrum with MIT License 4 votes vote down vote up
private List<TestRule> getClassRules() {
  return Stream.concat(
      testClass.getAnnotatedMethodValues(null, ClassRule.class, TestRule.class).stream(),
      testClass.getAnnotatedFieldValues(null, ClassRule.class, TestRule.class).stream())
      .collect(Collectors.toList());
}
 
Example #7
Source File: JUnitCustomRunnerTestUnitFinderTest.java    From pitest with Apache License 2.0 4 votes vote down vote up
@ClassRule
public static TestRule rule() {
  return new ExternalResource() {
  };
}
 
Example #8
Source File: AbstractTestSpringConfigurationFileAutoDetectingTest.java    From logback-access-spring-boot-starter with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a class test rule.
 *
 * @return a class test rule.
 */
@ClassRule
public static TestRule classRule() {
    return new ClassPathRule(AbstractTestSpringConfigurationFileAutoDetectingTest.class);
}
 
Example #9
Source File: AbstractTestConfigurationFileInvalidTest.java    From logback-access-spring-boot-starter with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a class test rule.
 *
 * @return a class test rule.
 */
@ClassRule
public static TestRule classRule() {
    return new ClassPathRule(AbstractTestConfigurationFileInvalidTest.class);
}
 
Example #10
Source File: AbstractMainSpringConfigurationFileAutoDetectingTest.java    From logback-access-spring-boot-starter with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a class test rule.
 *
 * @return a class test rule.
 */
@ClassRule
public static TestRule classRule() {
    return new ClassPathRule(AbstractMainSpringConfigurationFileAutoDetectingTest.class);
}
 
Example #11
Source File: AbstractTestSpringConfigurationFileInvalidTest.java    From logback-access-spring-boot-starter with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a class test rule.
 *
 * @return a class test rule.
 */
@ClassRule
public static TestRule classRule() {
    return new ClassPathRule(AbstractTestSpringConfigurationFileInvalidTest.class);
}
 
Example #12
Source File: AbstractTestConfigurationFileAutoDetectingTest.java    From logback-access-spring-boot-starter with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a class test rule.
 *
 * @return a class test rule.
 */
@ClassRule
public static TestRule classRule() {
    return new ClassPathRule(AbstractTestConfigurationFileAutoDetectingTest.class);
}
 
Example #13
Source File: AbstractMainConfigurationFileAutoDetectingTest.java    From logback-access-spring-boot-starter with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a class test rule.
 *
 * @return a class test rule.
 */
@ClassRule
public static TestRule classRule() {
    return new ClassPathRule(AbstractMainConfigurationFileAutoDetectingTest.class);
}
 
Example #14
Source File: AbstractMainConfigurationFileInvalidTest.java    From logback-access-spring-boot-starter with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a class test rule.
 *
 * @return a class test rule.
 */
@ClassRule
public static TestRule classRule() {
    return new ClassPathRule(AbstractMainConfigurationFileInvalidTest.class);
}
 
Example #15
Source File: AbstractMainSpringConfigurationFileInvalidTest.java    From logback-access-spring-boot-starter with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a class test rule.
 *
 * @return a class test rule.
 */
@ClassRule
public static TestRule classRule() {
    return new ClassPathRule(AbstractMainSpringConfigurationFileInvalidTest.class);
}