private Statement withLogging(final FrameworkMethod method, Object target, Statement statement) { final AtomicLong time = new AtomicLong(); List<TestRule> rules = new ArrayList<>(); rules.add(new ExternalResource() { @Override protected void before() throws Throwable { if (LOG.isDebugEnabled()) { time.set(System.currentTimeMillis()); LOG.debug("Beginning [" + method.getDeclaringClass().getCanonicalName() + "." + method.getName() + "]"); } } @Override protected void after() { if (LOG.isDebugEnabled()) { LOG.debug("Completed [" + method.getDeclaringClass().getCanonicalName() + "." + method.getName() + "] in [" + (System.currentTimeMillis() - time.get()) + "] ms"); } } }); return new RunRules(statement, rules, getDescription()); }
/** * Creates a {@link RuleChain} with {@link NucleusWithModules} as the first rule, so * that it gets started before any attempt to load test data, or resolve components. * Subsequent @NucleusWithXXX annotations, used for setting up test data, are added after * {@link NucleusWithModules}, as are {@link NucleusComponent} rules. * * @param statement * @param testNucleus * @return */ protected Statement createRuleChain(Statement statement, NucleusRequired testNucleus) { List<TestRule> chainedRules = new ArrayList<TestRule>(1); RuleChain chain = RuleChain.outerRule(new NucleusWithModules(testNucleus.modules(), testNucleus.isUseTestConfigLayer(), getTestClass().getJavaClass())); for (TestRule dataRule : classDataRules()) { chain = chain.around(dataRule); } chainedRules.add(chain); return new RunRules(statement, chainedRules, getDescription()); }
@Override protected Statement withBefores(FrameworkMethod method, Object target, Statement statement) { @SuppressWarnings("deprecation") Statement junitBefores = super.withBefores(method, target, statement); List<TestRule> chainedRules = new ArrayList<TestRule>(1); RuleChain chain = RuleChain.emptyRuleChain(); for (TestRule nucleusComponentInjectionRule : nucleusComponenInjectionRules()) { chain = chain.around(nucleusComponentInjectionRule); } chainedRules.add(chain); return new RunRules(junitBefores, chainedRules, getDescription()); }
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 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))); }
private Statement withTestRules(FrameworkMethod method, List<TestRule> testRules, Statement statement) { return testRules.isEmpty() ? statement : new RunRules(statement, testRules, describeChild(method)); }
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); }
private Statement withClassRules(Statement statement) { List<TestRule> classRules = classRules(); return classRules.isEmpty() ? statement : new RunRules(statement, classRules, getDescription()); }
/** * Returns a {@link Statement}: apply all non-static {@code Value} fields * annotated with {@link org.junit.Rule}. * * @param method テストメソッド * @param testRules ルール * @param statement The base statement * @return a RunRules statement if any class-level {@link org.junit.Rule}s * are found, or the base statement */ private Statement withTestRules( final FrameworkMethod method, final List<TestRule> testRules, final Statement statement) { if (testRules.isEmpty()) { return statement; } else { return new RunRules(statement, testRules, describeChild(method)); } }
/** * Returns a {@link Statement}: apply all * static fields assignable to {@link TestRule} * annotated with {@link ClassRule}. * * @param statement the base statement * @return a RunRules statement if any class-level {@link Rule}s are * found, or the base statement */ private Statement withClassRules(Statement statement) { List<TestRule> classRules = classRules(); return classRules.isEmpty() ? statement : new RunRules(statement, classRules, getDescription()); }
/** * Returns a {@link Statement}: apply all * static fields assignable to {@link TestRule} * annotated with {@link ClassRule}. * * @param statement the base statement * @param testClass * @param description the description to pass to the {@link Rule}s * @return a RunRules statement if any class-level {@link Rule}s are * found, or the base statement * * @see ParentRunner#withClassRules(org.junit.runners.model.Statement) */ public static Statement withClassRules(Statement statement, TestClass testClass, Description description) { List<TestRule> classRules = getClassRules(testClass); return classRules.isEmpty() ? statement : new RunRules(statement, classRules, description); }
/** * Returns a {@link Statement}: apply all non-static fields annotated with * {@link Rule}. * <p> * This method has a slighty changed signature compared to the original * BlockJUnit4ClassRunner#withTestRules(FrameworkMethod, List, Statement), * replacing the {@link FrameworkMethod} parameter with a * {@link Description}. * * @param description * The description passed to the {@link Rule} * @param statement * The base statement * * @return a RunRules statement if any class-level {@link Rule}s are found, * or the base statement * * @see BlockJUnit4ClassRunner */ public static Statement withTestRules(List<TestRule> testRules, Description description, Statement statement) { return testRules.isEmpty() ? statement : new RunRules(statement, testRules, description); }
/** * Returns a {@link org.junit.runners.model.Statement}: apply all * static fields assignable to {@link org.junit.rules.TestRule} * annotated with {@link org.junit.ClassRule}. * * @param statement the base statement * @return a RunRules statement if any class-level {@link org.junit.Rule}s are * found, or the base statement */ private Statement withClassRules(Statement statement) { List<TestRule> classRules = classRules(); return classRules.isEmpty() ? statement : new RunRules(statement, classRules, getDescription()); }
/** * Returns a {@link org.junit.runners.model.Statement}: apply all non-static value fields * annotated with {@link org.junit.Rule}. * * @param statement The base statement * @return a RunRules statement if any class-level {@link org.junit.Rule}s are * found, or the base statement */ private Statement withTestRules(FrameworkMethod method, List<TestRule> testRules, Statement statement) { return testRules.isEmpty() ? statement : new RunRules(statement, testRules, describeChild(method)); }
/** * Returns a {@link Statement}: apply all * static fields assignable to {@link TestRule} * annotated with {@link ClassRule}. * * @param statement * the base statement * @return a RunRules statement if any class-level {@link Rule}s are * found, or the base statement */ private Statement withClassRules(Statement statement) { List<TestRule> classRules= classRules(); return classRules.isEmpty() ? statement : new RunRules(statement, classRules, getDescription()); }
/** * Returns a {@link Statement}: apply all non-static fields * annotated with {@link Rule}. * * @param statement The base statement * @return a RunRules statement if any class-level {@link Rule}s are * found, or the base statement */ private Statement withTestRules(FrameworkMethod method, List<TestRule> testRules, Statement statement) { return testRules.isEmpty() ? statement : new RunRules(statement, testRules, describeChild(method)); }
/** * Create a {@link NucleusWithTransaction} {@link TestRule} and add it to the * list of rules for the test class. * * @param method * @param statement * @return */ protected Statement withTransaction(FrameworkMethod method, Statement statement) { List<TestRule> transactionRules = new ArrayList<TestRule>(1); transactionRules.add(new NucleusWithTransaction()); return new RunRules(statement, transactionRules, describeChild(method)); }
/** * Add all Rules, created by @NucleusWithXXX annotations, to the list of test rules * for this class. * * @param statement * @return */ protected Statement withClassData(Statement statement) { List<TestRule> classDataRules = classDataRules(); return classDataRules.isEmpty() ? statement : new RunRules(statement, classDataRules, getDescription()); }
/** * Looks for {@link NucleusComponent} annotations on the test class * and creates a {@link TestRule} for each field with the annotation. * The rule ensures that the field is injected with the correct * component before any tests are run. * * @param statement the current statement * @return a statement that has been updated with nucleus compoent * injection rules, or the current statement if no NucleusComponent * annotations were found. */ protected Statement withNucleusComponentRules(Statement statement) { List<TestRule> nucleusComponentRules = nucleusComponenInjectionRules(); return nucleusComponentRules.isEmpty() ? statement : new RunRules(statement, nucleusComponentRules, getDescription()); }
/** * Returns a {@link Statement}: apply all non-static {@link Value} fields * annotated with {@link Rule}. * * @param statement The base statement * @return a RunRules statement if any class-level {@link Rule}s are * found, or the base statement */ private Statement withTestRules(FrameworkMethod method, List<TestRule> testRules, Statement statement) { return testRules.isEmpty() ? statement : new RunRules(statement, testRules, describeChild(method)); }