/** * 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); }
/** * @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; }
/** * Create a RuleChain containing a RepositoryFixture for each of the repositories we are testing against * * @return a RuleChain */ @ClassRule public static TestRule getClassRule() { // Put all the fixtures into one chain rule because we want to set up all the repositories at the start of the test, test with them and tear them down at the end RuleChain chain = RuleChain.emptyRuleChain(); for (RepoData repoData : getRepoDataList()) { chain = chain.around(repoData.fixture); } return chain; }
/** * 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!"); } } }
@Override public void testStarted(Description description) throws Exception { 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()); }
/** * @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 = fTestClass.getAnnotatedMethodValues(null, ClassRule.class, TestRule.class); result.addAll(fTestClass.getAnnotatedFieldValues(null, ClassRule.class, TestRule.class)); return result; }
public Statement createStatement(final TestClass testClass, final Statement next, final Description description, final RunNotifier notifier) { final List<TestRule> classRules = new ArrayList<TestRule>(); classRules.addAll(testClass.getAnnotatedMethodValues(null, ClassRule.class, TestRule.class)); classRules.addAll(testClass.getAnnotatedFieldValues(null, ClassRule.class, TestRule.class)); return classRules.isEmpty() ? next : new RunRules(next, classRules, description); }
@Test public void givenTestClassWithoutRuleAnnotations_returnsNextStatement() throws Exception { when(testClass.getAnnotatedMethodValues(null, ClassRule.class, TestRule.class)).thenReturn(Collections.EMPTY_LIST); when(testClass.getAnnotatedFieldValues(null, ClassRule.class, TestRule.class)).thenReturn(Collections.EMPTY_LIST); Statement statement = builder.createStatement(testClass, next, description, notifier); assertThat(statement, is(equalTo(next))); }
@Test public void givenTestClassWithRuleAnnotatedMethods_returnsRunRulesStatement() throws Exception { List<TestRule> methods = Arrays.asList(rule1, rule2); when(testClass.getAnnotatedMethodValues(null, ClassRule.class, TestRule.class)).thenReturn(methods); when(testClass.getAnnotatedFieldValues(null, ClassRule.class, TestRule.class)).thenReturn(Collections.EMPTY_LIST); Statement actual = builder.createStatement(testClass, next, description, notifier); assertThat(actual, is(instanceOf(RunRules.class))); }
@Test public void givenTestClassWithRuleAnnotatedFields_returnsRunRulesStatement() throws Exception { List<TestRule> fields = Arrays.asList(rule1, rule2); when(testClass.getAnnotatedMethodValues(null, ClassRule.class, TestRule.class)).thenReturn(Collections.EMPTY_LIST); when(testClass.getAnnotatedFieldValues(null, ClassRule.class, TestRule.class)).thenReturn(fields); Statement actual = builder.createStatement(testClass, next, description, notifier); assertThat(actual, is(instanceOf(RunRules.class))); }
@Test public void givenTestClassWithRuleAnnotatedMethodsAndFields_returnsRunRulesStatement() throws Exception { List<TestRule> methods = Arrays.asList(rule1); List<TestRule> fields = Arrays.asList(rule2); when(testClass.getAnnotatedMethodValues(null, ClassRule.class, TestRule.class)).thenReturn(methods); when(testClass.getAnnotatedFieldValues(null, ClassRule.class, TestRule.class)).thenReturn(fields); Statement actual = builder.createStatement(testClass, next, description, notifier); assertThat(actual, is(instanceOf(RunRules.class))); }
@Override @SuppressWarnings("deprecation") protected Statement methodBlock(FrameworkMethod method) { Object test; try { test = new ReflectiveCallable() { @Override protected Object runReflectiveCall() throws Throwable { return createTest(); } }.run(); } catch (Throwable e) { return new Fail(e); } boolean validated = true; for (CdhServer cdhServer : getTestClass().getAnnotatedFieldValues(test, ClassRule.class, CdhServer.class)) { validated = validated && cdhServer.isValid(); if (validated) { for (CdhServer cdhServerDependency : cdhServer.getDependencies()) { validated = validated && cdhServerDependency.isValid(); } } } Statement statement; if (validated) { statement = methodInvoker(method, test); statement = withLogging(method, test, statement); statement = possiblyExpectingExceptions(method, test, statement); statement = withPotentialTimeout(method, test, statement); statement = withBefores(method, test, statement); statement = withAfters(method, test, statement); statement = withServerRules(method, test, statement); statement = withRules(method, test, statement); } else { statement = new Statement() { @Override public void evaluate() throws Throwable { if (LOG.isWarnEnabled()) { LOG.warn("Skipping [" + method.getDeclaringClass().getCanonicalName() + "." + method.getName() + "], invalid classpath"); } } }; } return statement; }
@ClassRule public static OrderTestRule orderMethod() { return new OrderTestRule("orderMethod"); }
@ClassRule public static Counter getCounter() { return counter; }
@ClassRule public static CustomCounter getCounter() { return counter; }
@ClassRule public static Dummy both() { countOfMethodCalls++; return new Dummy(); }
@ClassRule protected static TestRule getTemporaryFolder() { return new TemporaryFolder(); }
@ClassRule public TestRule getTemporaryFolder() { return new TemporaryFolder(); }
protected Statement prepareRules(TestClass extension, Statement base, Description description) { List<TestRule> rules = extension.getAnnotatedFieldValues(null, Rule.class, TestRule.class); rules.addAll(extension.getAnnotatedFieldValues(null, ClassRule.class, TestRule.class)); return new RunRules(base, rules, description); }
@ClassRule public static TestRule rule() { return new ExternalResource() { }; }
/** * @param testClass * @return the {@code ClassRule}s that can transform the block that runs * each method in the tested class. * * @see ParentRunner#classRules() */ public static List<TestRule> getClassRules(TestClass testClass) { List<TestRule> result = testClass.getAnnotatedMethodValues(null, ClassRule.class, TestRule.class); result.addAll(testClass.getAnnotatedFieldValues(null, ClassRule.class, TestRule.class)); return result; }
/** * @return the {@code ClassRule}s that can transform the block that runs * each method in the tested class. */ protected List<TestRule> classRules() { return fTestClass.getAnnotatedFieldValues(null, ClassRule.class, TestRule.class); }
/** * Creates a class test rule. * * @return a class test rule. */ @ClassRule public static TestRule classRule() { return new ClassPathRule(AbstractTestSpringConfigurationFileAutoDetectingTest.class); }