當前位置: 首頁>>代碼示例>>Java>>正文


Java TestRule類代碼示例

本文整理匯總了Java中org.junit.rules.TestRule的典型用法代碼示例。如果您正苦於以下問題:Java TestRule類的具體用法?Java TestRule怎麽用?Java TestRule使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


TestRule類屬於org.junit.rules包,在下文中一共展示了TestRule類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: buildStatementWithTestRules

import org.junit.rules.TestRule; //導入依賴的package包/類
/**
 * Extends a given {@link Statement} for a {@link TestClass} with the evaluation of
 * {@link TestRule}, {@link ClassRule}, {@link Before} and {@link After}.
 * <p>
 * Therefore the test class will be instantiated and parameters will be injected with the same
 * mechanism as in {@link Parameterized}.
 * 
 * Implementation has been extracted from BlockJUnit4ClassRunner#methodBlock(FrameworkMethod).
 * 
 * @param baseStatementWithChildren - A {@link Statement} that includes execution of the test's
 *        children
 * @param testClass - The {@link TestClass} of the test.
 * @param description - The {@link Description} will be passed to the {@link Rule}s and
 *        {@link ClassRule}s.
 * @param singleParameter - The parameters will be injected in attributes annotated with
 *        {@link Parameterized.Parameter} or passed to the constructor otherwise.
 * 
 * @see BlockJUnit4ClassRunnerWithParameters#createTest()
 * @see BlockJUnit4ClassRunner#methodBlock(FrameworkMethod)
 */
public static Statement buildStatementWithTestRules(Statement baseStatementWithChildren, final TestClass testClass, Description description,
        final Object[] singleParameter) {
    final Object test;
    try {
        test = new ReflectiveCallable() {
            protected Object runReflectiveCall() throws Throwable {
                return createInstanceOfParameterizedTest(testClass, singleParameter);
            }
        }.run();
    } catch (Throwable e) {
        return new Fail(e);
    }

    List<TestRule> testRules = BlockJUnit4ClassRunnerUtil.getTestRules(test, testClass);
    Statement statement = BlockJUnit4ClassRunnerUtil.withTestRules(testRules, description, baseStatementWithChildren);

    statement = ParentRunnerUtil.withBeforeClasses(statement, testClass);
    statement = ParentRunnerUtil.withAfterClasses(statement, testClass);
    statement = ParentRunnerUtil.withClassRules(statement, testClass, description);
    return statement;
}
 
開發者ID:PeterWippermann,項目名稱:parameterized-suite,代碼行數:42,代碼來源:BlockJUnit4ClassRunnerWithParametersUtil.java

示例2: applyRuleToLastStatement

import org.junit.rules.TestRule; //導入依賴的package包/類
@Override
protected Statement applyRuleToLastStatement(final Method method, final Object testInstance, Field field,
        final LastRuleTestExecutorStatement lastStatement) throws IllegalAccessException {
    final Object fieldValue = field.get(testInstance);
    final Statement statement;
    if (fieldValue instanceof MethodRule) {
        // the MethodRule is known by junit 4.9 -> delegate to super-class
        statement = super.applyRuleToLastStatement(method, testInstance, field, lastStatement);
    } else if (fieldValue instanceof TestRule){
        TestRule rule = (TestRule) fieldValue;
        statement = rule.apply(lastStatement, description);
    } else {
        throw new IllegalStateException("Can only handle MethodRule and TestRule");
    }
    return statement;
}
 
開發者ID:awenblue,項目名稱:powermock,代碼行數:17,代碼來源:PowerMockJUnit49RunnerDelegateImpl.java

示例3: openAndCloseDriver

import org.junit.rules.TestRule; //導入依賴的package包/類
@Rule
public final TestRule openAndCloseDriver() {
    return (base, description) -> new Statement() {

        @Override
        public void evaluate() throws Throwable {
            driver = createDriver();
            try {
                base.evaluate();
            } finally {
                if (closeDriver)
                    try {
                        driver.close();
                    } catch (Throwable t) {
                        // swallow
                    }
            }
        }
    };
}
 
開發者ID:ruediste,項目名稱:rise,代碼行數:21,代碼來源:WebTestBase.java

示例4: withLogging

import org.junit.rules.TestRule; //導入依賴的package包/類
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());
}
 
開發者ID:ggear,項目名稱:cloudera-framework,代碼行數:23,代碼來源:TestRunner.java

示例5: getHiveRunnerConfigRule

import org.junit.rules.TestRule; //導入依賴的package包/類
private TestRule getHiveRunnerConfigRule(final Object target) {
    return new TestRule() {
        @Override
        public Statement apply(Statement base, Description description) {
            Set<Field> fields = ReflectionUtils.getAllFields(target.getClass(),
                    Predicates.and(
                            withAnnotation(HiveRunnerSetup.class),
                            withType(HiveRunnerConfig.class)));

            Preconditions.checkState(fields.size() <= 1,
                    "Exact one field of type HiveRunnerConfig should to be annotated with @HiveRunnerSetup");

            /*
             Override the config with test case config. Taking care to not replace the config instance since it
              has been passes around and referenced by some of the other test rules.
              */
            if (!fields.isEmpty()) {
                config.override(ReflectionUtils
                        .getFieldValue(target, fields.iterator().next().getName(), HiveRunnerConfig.class));
            }

            return base;
        }
    };
}
 
開發者ID:klarna,項目名稱:HiveRunner,代碼行數:26,代碼來源:StandaloneHiveRunner.java

示例6: classRules

import org.junit.rules.TestRule; //導入依賴的package包/類
@Override
protected List<TestRule> classRules() {
  List<TestRule> rules = super.classRules();
  rules.add(new ExternalResource() {
    @Override
    protected void before() throws Throwable {
      beforeClass();
      super.before();
    }

    @Override
    protected void after() {
      beforeAfterClass();
      super.after();
      afterClass();
    }
  });
  return rules;
}
 
開發者ID:bytor99999,項目名稱:vertx-junit-annotations,代碼行數:20,代碼來源:JUnit4ClassRunnerAdapter.java

示例7: createTestRule

import org.junit.rules.TestRule; //導入依賴的package包/類
public TestRule createTestRule() {
  final TemporaryFolder tempFolder = new TemporaryFolder();

  return RuleChain
    .outerRule(tempFolder)
    .around(new ExternalResource() {

      TomcatServerBootstrap bootstrap = new JerseyTomcatServerBootstrap(webXmlResource);

      protected void before() throws Throwable {
        bootstrap.setWorkingDir(tempFolder.getRoot().getAbsolutePath());
        bootstrap.start();
      }

      protected void after() {
        bootstrap.stop();
      }
    });
}
 
開發者ID:camunda,項目名稱:camunda-bpm-platform,代碼行數:20,代碼來源:JerseySpecifics.java

示例8: createTestRule

import org.junit.rules.TestRule; //導入依賴的package包/類
public TestRule createTestRule() {
  final TemporaryFolder tempFolder = new TemporaryFolder();

  return RuleChain
    .outerRule(tempFolder)
    .around(new ExternalResource() {

      WinkTomcatServerBootstrap bootstrap = new WinkTomcatServerBootstrap(webXmlResource);

      protected void before() throws Throwable {
        bootstrap.setWorkingDir(tempFolder.getRoot().getAbsolutePath());
        bootstrap.start();
      }

      protected void after() {
        bootstrap.stop();
      }
    });
}
 
開發者ID:camunda,項目名稱:camunda-bpm-platform,代碼行數:20,代碼來源:WinkSpecifics.java

示例9: createTestRule

import org.junit.rules.TestRule; //導入依賴的package包/類
public TestRule createTestRule() {
  final TemporaryFolder tempFolder = new TemporaryFolder();

  return RuleChain
    .outerRule(tempFolder)
    .around(new ExternalResource() {

      TomcatServerBootstrap bootstrap = new CXFTomcatServerBootstrap(webXmlResource);

      protected void before() throws Throwable {
        bootstrap.setWorkingDir(tempFolder.getRoot().getAbsolutePath());
        bootstrap.start();
      }

      protected void after() {
        bootstrap.stop();
      }
    });
}
 
開發者ID:camunda,項目名稱:camunda-bpm-platform,代碼行數:20,代碼來源:CXFSpecifics.java

示例10: createTestRule

import org.junit.rules.TestRule; //導入依賴的package包/類
public TestRule createTestRule() {
  final TemporaryFolder tempFolder = new TemporaryFolder();

  return RuleChain
    .outerRule(tempFolder)
    .around(new ExternalResource() {

      ResteasyTomcatServerBootstrap bootstrap = new ResteasyTomcatServerBootstrap(webXmlResource);

      protected void before() throws Throwable {
        bootstrap.setWorkingDir(tempFolder.getRoot().getAbsolutePath());
        bootstrap.start();
      }

      protected void after() {
        bootstrap.stop();
      }
    });
}
 
開發者ID:camunda,項目名稱:camunda-bpm-platform,代碼行數:20,代碼來源:ResteasySpecifics.java

示例11: maintainer

import org.junit.rules.TestRule; //導入依賴的package包/類
/**
 * A JUnit {@link TestRule} that stops tests from interfering with one 
 * another. JUnit will automatically set up/clean up the catalog when this
 * rule is used. <br/>
 * Usage:<br/>
 * {@code @Rule public final TestRule catalogMaintainer = TestCatalogModel.maintainer()}
 * @return the TestRule
 */
public static TestRule maintainer() {
    return new ExternalResource() {

        @Override
        protected void after() {
            getDefault().clearDocumentPool();
        }
    
    };
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:19,代碼來源:TestCatalogModel.java

示例12: getLinkedRule

import org.junit.rules.TestRule; //導入依賴的package包/類
/**
 * Get the test rule of the specified type that's attached to the rule chain.
 * 
 * @param <T> test rule type
 * @param testRuleType test rule type
 * @return {@link ScreenshotCapture} test rule
 */
public <T extends TestRule> T getLinkedRule(Class<T> testRuleType) {
    Optional<T> optional = RuleChainWalker.getAttachedRule(ruleChain, testRuleType);
    if (optional.isPresent()) {
        return optional.get();
    }
    throw new IllegalStateException(testRuleType.getSimpleName() + " test rule wasn't found on the rule chain");
}
 
開發者ID:Nordstrom,項目名稱:Selenium-Foundation,代碼行數:15,代碼來源:JUnitBase.java

示例13: getTestRules

import org.junit.rules.TestRule; //導入依賴的package包/類
@Override
protected List<TestRule> getTestRules(Object target) {
    if (isFocusEnabled()) {
        List<TestRule> old = super.getTestRules(target);
        old.removeIf(rule -> rule instanceof Timeout);
        return old;
    } else {
        return super.getTestRules(target);
    }
}
 
開發者ID:altiplanogao,項目名稱:io-comparison,代碼行數:11,代碼來源:FocusRunner.java

示例14: apply

import org.junit.rules.TestRule; //導入依賴的package包/類
private <R extends TestRule> Statement apply(Statement base, Description description,
		Delegate<R> delegate) {
	final R rule = delegate.createRule();
	Statement statement = new Statement() {

		@Override
		public void evaluate() throws Throwable {
			ArtifactoryServerConnection.this.serverFactory = (artifactory) -> delegate
					.getArtifactoryServer(rule, artifactory);
			base.evaluate();
		}

	};
	return rule.apply(statement, description);
}
 
開發者ID:spring-io,項目名稱:artifactory-resource,代碼行數:16,代碼來源:ArtifactoryServerConnection.java

示例15: apply

import org.junit.rules.TestRule; //導入依賴的package包/類
@Override
public Statement apply(Statement base, final Description description) {
  for (TestRule each : this.rules) {
    base = each.apply(base, description);
  }
  return base;
}
 
開發者ID:ampool,項目名稱:monarch,代碼行數:8,代碼來源:RuleList.java


注:本文中的org.junit.rules.TestRule類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。