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);
}
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();
}
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());
}
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);
}
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();
}
Aggregations