Search in sources :

Example 16 with Request

use of org.junit.runner.Request in project powermock by powermock.

the class GitHub352Test method testCountShouldBe3WhenRunWithPowerMockRunner.

@Test
public void testCountShouldBe3WhenRunWithPowerMockRunner() {
    JUnitCore jUnitCore = new JUnitCore();
    Request request = new Request() {

        @Override
        public Runner getRunner() {
            try {
                return new PowerMockRunner(MyTest.class);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    };
    Result result = jUnitCore.run(request);
    int testCount = result.getRunCount();
    assertThat(testCount).describedAs("Test count not match to expected.", 3).isEqualTo(3);
}
Also used : JUnitCore(org.junit.runner.JUnitCore) Request(org.junit.runner.Request) PowerMockRunner(org.powermock.modules.junit4.PowerMockRunner) Result(org.junit.runner.Result) Test(org.junit.Test)

Example 17 with Request

use of org.junit.runner.Request in project bazel by bazelbuild.

the class CancellableRequestFactoryTest method testCancelRunAfterStarting.

@Test
public void testCancelRunAfterStarting() throws Exception {
    final CountDownLatch testStartLatch = new CountDownLatch(1);
    final CountDownLatch testContinueLatch = new CountDownLatch(1);
    final AtomicBoolean secondTestRan = new AtomicBoolean(false);
    // Simulates a test that hangs
    FakeRunner blockingRunner = new FakeRunner("blocks", new Runnable() {

        @Override
        public void run() {
            testStartLatch.countDown();
            try {
                testContinueLatch.await(1, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                throw new RuntimeException("Timed out waiting for signal to continue test", e);
            }
        }
    });
    // A runner that should never run its test
    FakeRunner secondRunner = new FakeRunner("shouldNotRun", new Runnable() {

        @Override
        public void run() {
            secondTestRan.set(true);
        }
    });
    RunnerSuite fakeSuite = new RunnerSuite(blockingRunner, secondRunner);
    final Request request = cancellableRequestFactory.createRequest(Request.runner(fakeSuite));
    ExecutorService executor = Executors.newSingleThreadExecutor();
    Future<Result> future = executor.submit(new Callable<Result>() {

        @Override
        public Result call() throws Exception {
            JUnitCore core = new JUnitCore();
            return core.run(request);
        }
    });
    // Simulate cancel being called in the middle of the test
    testStartLatch.await(1, TimeUnit.SECONDS);
    cancellableRequestFactory.cancelRun();
    testContinueLatch.countDown();
    try {
        future.get(10, TimeUnit.SECONDS);
        fail("exception expected");
    } catch (ExecutionException e) {
        Throwable runnerException = e.getCause();
        assertTrue(runnerException instanceof RuntimeException);
        assertEquals("Test run interrupted", runnerException.getMessage());
        assertTrue(runnerException.getCause() instanceof StoppedByUserException);
    }
    executor.shutdownNow();
}
Also used : JUnitCore(org.junit.runner.JUnitCore) Request(org.junit.runner.Request) CountDownLatch(java.util.concurrent.CountDownLatch) AssumptionViolatedException(org.junit.internal.AssumptionViolatedException) ExecutionException(java.util.concurrent.ExecutionException) StoppedByUserException(org.junit.runner.notification.StoppedByUserException) Result(org.junit.runner.Result) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ExecutorService(java.util.concurrent.ExecutorService) StoppedByUserException(org.junit.runner.notification.StoppedByUserException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 18 with Request

use of org.junit.runner.Request in project bazel by bazelbuild.

the class CancellableRequestFactoryTest method testFailingRun.

@Test
public void testFailingRun() {
    final AtomicBoolean testRan = new AtomicBoolean(false);
    final RuntimeException expectedFailure = new RuntimeException();
    // A runner that should run its test
    FakeRunner runner = new FakeRunner("shouldRun", new Runnable() {

        @Override
        public void run() {
            testRan.set(true);
            throw expectedFailure;
        }
    });
    Request request = cancellableRequestFactory.createRequest(Request.runner(runner));
    JUnitCore core = new JUnitCore();
    Result result = core.run(request);
    assertTrue(testRan.get());
    assertEquals(1, result.getRunCount());
    assertEquals(1, result.getFailureCount());
    assertSame(expectedFailure, result.getFailures().get(0).getException());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) JUnitCore(org.junit.runner.JUnitCore) Request(org.junit.runner.Request) Result(org.junit.runner.Result) Test(org.junit.Test)

Example 19 with Request

use of org.junit.runner.Request in project bazel by bazelbuild.

the class JUnit4TestXmlListenerTest method signalHandlerWritesXml.

@Test
public void signalHandlerWritesXml() throws Exception {
    TestSuiteModelSupplier mockModelSupplier = mock(TestSuiteModelSupplier.class);
    TestSuiteModel mockModel = mock(TestSuiteModel.class);
    CancellableRequestFactory mockRequestFactory = mock(CancellableRequestFactory.class);
    OutputStream mockXmlStream = mock(OutputStream.class);
    JUnit4TestXmlListener listener = new JUnit4TestXmlListener(mockModelSupplier, mockRequestFactory, fakeSignalHandlers, mockXmlStream, errPrintStream);
    Request request = Request.classWithoutSuiteMethod(PassingTest.class);
    Description suiteDescription = request.getRunner().getDescription();
    when(mockModelSupplier.get()).thenReturn(mockModel);
    listener.testRunStarted(suiteDescription);
    assertEquals(1, fakeSignalHandlers.handlers.size());
    fakeSignalHandlers.handlers.get(0).handle(new Signal("TERM"));
    String errOutput = errStream.toString(CHARSET);
    assertTrue("expected signal name in stderr", errOutput.contains("SIGTERM"));
    assertTrue("expected message in stderr", errOutput.contains("Done writing test XML"));
    InOrder inOrder = inOrder(mockRequestFactory, mockModel);
    inOrder.verify(mockRequestFactory).cancelRun();
    inOrder.verify(mockModel).testRunInterrupted();
    inOrder.verify(mockModel).writeAsXml(mockXmlStream);
}
Also used : Signal(sun.misc.Signal) Description(org.junit.runner.Description) InOrder(org.mockito.InOrder) TestSuiteModel(com.google.testing.junit.runner.model.TestSuiteModel) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) Request(org.junit.runner.Request) Test(org.junit.Test)

Example 20 with Request

use of org.junit.runner.Request in project bazel by bazelbuild.

the class JUnit4TestModelBuilderTest method testTouchesShardFileWhenShardingEnabled.

@Test
public void testTouchesShardFileWhenShardingEnabled() {
    Class<?> testClass = SampleTestCaseWithTwoTests.class;
    Request request = Request.classWithoutSuiteMethod(testClass);
    ShardingEnvironment mockShardingEnvironment = mock(ShardingEnvironment.class);
    ShardingFilters shardingFilters = new ShardingFilters(mockShardingEnvironment, DEFAULT_SHARDING_STRATEGY);
    JUnit4TestModelBuilder modelBuilder = builder(request, testClass.getCanonicalName(), mockShardingEnvironment, shardingFilters, xmlResultWriter);
    when(mockShardingEnvironment.isShardingEnabled()).thenReturn(true);
    when(mockShardingEnvironment.getTotalShards()).thenReturn(2);
    modelBuilder.get();
    verify(mockShardingEnvironment).touchShardFile();
}
Also used : Request(org.junit.runner.Request) ShardingFilters(com.google.testing.junit.runner.sharding.ShardingFilters) StubShardingEnvironment(com.google.testing.junit.runner.sharding.testing.StubShardingEnvironment) ShardingEnvironment(com.google.testing.junit.runner.sharding.ShardingEnvironment) Test(org.junit.Test)

Aggregations

Request (org.junit.runner.Request)54 Test (org.junit.Test)37 JUnitCore (org.junit.runner.JUnitCore)32 Result (org.junit.runner.Result)22 Description (org.junit.runner.Description)20 TestSuiteModel (com.google.testing.junit.runner.model.TestSuiteModel)8 Runner (org.junit.runner.Runner)8 PrintableResult.testResult (org.junit.experimental.results.PrintableResult.testResult)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 RunListener (org.junit.runner.notification.RunListener)6 OutputStream (java.io.OutputStream)5 CategoryFilter (org.junit.experimental.categories.Categories.CategoryFilter)5 Filter (org.junit.runner.manipulation.Filter)5 Failure (org.junit.runner.notification.Failure)5 IOException (java.io.IOException)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 InOrder (org.mockito.InOrder)4 ArrayList (java.util.ArrayList)3 Computer (org.junit.runner.Computer)3 TestNode (com.google.testing.junit.runner.model.TestNode)2