Search in sources :

Example 1 with RunnerBuilder

use of org.junit.runners.model.RunnerBuilder in project buck by facebook.

the class TimeoutTest method doTestUsingThreadGuardedTestClass.

private void doTestUsingThreadGuardedTestClass() throws InitializationError {
    Class<?> testClass = ThreadGuardedTest.class;
    RunnerBuilder builder = new RunnerBuilder() {

        @Override
        public Runner runnerForClass(Class<?> clazz) throws Throwable {
            return new BuckBlockJUnit4ClassRunner(clazz, /* defaultTestTimeoutMillis */
            500);
        }
    };
    Runner suite = new Computer().getSuite(builder, new Class<?>[] { testClass });
    Request request = Request.runner(suite);
    final Set<Result> results = Sets.newHashSet();
    JUnitCore core = new JUnitCore();
    core.addListener(new RunListener() {

        @Override
        public void testRunFinished(Result result) throws Exception {
            results.add(result);
        }
    });
    core.run(request);
    Result result = Iterables.getOnlyElement(results);
    assertEquals(3, result.getRunCount());
    assertEquals(2, result.getFailureCount());
    // The order in which the tests were run doesn't matter. What matters is that we see our
    // expected messages.
    Set<String> messages = result.getFailures().stream().map(Failure::getMessage).collect(MoreCollectors.toImmutableSet());
    assertEquals("Should contain explicit call to fail() from failingTestsAreReported() and " + "the timeout message from testsMayTimeOut().", ImmutableSet.of("This is expected", "test testsMayTimeOut timed out after 500 milliseconds"), messages);
}
Also used : Runner(org.junit.runner.Runner) JUnitCore(org.junit.runner.JUnitCore) Request(org.junit.runner.Request) Result(org.junit.runner.Result) RunListener(org.junit.runner.notification.RunListener) RunnerBuilder(org.junit.runners.model.RunnerBuilder) Computer(org.junit.runner.Computer)

Aggregations

Computer (org.junit.runner.Computer)1 JUnitCore (org.junit.runner.JUnitCore)1 Request (org.junit.runner.Request)1 Result (org.junit.runner.Result)1 Runner (org.junit.runner.Runner)1 RunListener (org.junit.runner.notification.RunListener)1 RunnerBuilder (org.junit.runners.model.RunnerBuilder)1