@Test public void reportFailureAfterTestCompleted() { AtomicReference<TestContext> testRef = new AtomicReference<>(); TestSuite suite = TestSuite.create("my_suite") .test("my_test_1", testRef::set) .test( "my_test_2", context -> { try { testRef.get().fail(); } catch (AssertionError e) { } }); TestReporter reporter = new TestReporter(); run(suite, reporter); reporter.await(); assertEquals(1, reporter.exceptions.size()); assertEquals(2, reporter.results.size()); TestResult result = reporter.results.get(0); assertEquals("my_test_1", result.name()); assertTrue(result.succeeded()); assertNull(result.failure()); result = reporter.results.get(1); assertEquals("my_test_2", result.name()); assertTrue(result.succeeded()); assertNull(result.failure()); }
private void failTest(Handler<TestContext> thrower) { AtomicReference<Throwable> failure = new AtomicReference<>(); TestSuite suite = TestSuite.create("my_suite") .test( "my_test", context -> { try { thrower.handle(context); } catch (Error | RuntimeException e) { failure.set(e); throw e; } }); TestReporter reporter = new TestReporter(); run(suite, reporter); reporter.await(); assertEquals(0, reporter.exceptions.size()); assertEquals(1, reporter.results.size()); TestResult result = reporter.results.get(0); assertEquals("my_test", result.name()); assertFalse(result.succeeded()); assertTrue(result.failed()); assertNotNull(result.failure()); assertSame(failure.get().getMessage(), result.failure().message()); assertSame(failure.get(), result.failure().cause()); }
@Test public void runTestWithFailBeforeAsync() throws Exception { AtomicReference<AssertionError> failure = new AtomicReference<>(); BlockingQueue<Async> queue = new ArrayBlockingQueue<>(1); TestSuite suite = TestSuite.create("my_suite") .test( "my_test", context -> { try { context.fail(); } catch (AssertionError e) { failure.set(e); } queue.add(context.async()); }); TestReporter reporter = new TestReporter(); run(suite, reporter); reporter.await(); Async async = queue.poll(2, TimeUnit.SECONDS); async.complete(); assertTrue(reporter.completed()); assertEquals(0, reporter.exceptions.size()); assertEquals(1, reporter.results.size()); TestResult result = reporter.results.get(0); assertEquals("my_test", result.name()); assertFalse(result.succeeded()); assertTrue(result.failed()); assertNotNull(result.failure()); assertSame(failure.get(), result.failure().cause()); }
@Test public void runTestWithFailAfterAsync() throws Exception { RuntimeException failure = new RuntimeException(); BlockingQueue<Async> queue = new ArrayBlockingQueue<>(1); TestSuite suite = TestSuite.create("my_suite") .test( "my_test", context -> { queue.add(context.async()); throw failure; }); TestReporter reporter = new TestReporter(); run(suite, reporter); reporter.await(); Async async = queue.poll(2, TimeUnit.SECONDS); async.complete(); assertTrue(reporter.completed()); assertEquals(0, reporter.exceptions.size()); assertEquals(1, reporter.results.size()); TestResult result = reporter.results.get(0); assertEquals("my_test", result.name()); assertFalse(result.succeeded()); assertTrue(result.failed()); assertNotNull(result.failure()); assertSame(failure, result.failure().cause()); }
@Test public void runTestWithAsyncCompletion() throws Exception { BlockingQueue<Async> queue = new ArrayBlockingQueue<>(1); AtomicInteger count = new AtomicInteger(); TestSuite suite = TestSuite.create("my_suite") .test( "my_test", context -> { count.compareAndSet(0, 1); queue.add(context.async()); }); TestReporter reporter = new TestReporter(); run(suite, reporter); Async async = queue.poll(2, TimeUnit.SECONDS); assertEquals(1, count.get()); assertFalse(reporter.completed()); completeAsync.accept(async); reporter.await(); assertTrue(reporter.completed()); assertEquals(0, reporter.exceptions.size()); assertEquals(1, reporter.results.size()); TestResult result = reporter.results.get(0); assertEquals("my_test", result.name()); assertTrue(result.succeeded()); assertFalse(result.failed()); assertNull(result.failure()); }
@Test public void runTest() { AtomicInteger count = new AtomicInteger(); AtomicBoolean sameContext = new AtomicBoolean(); TestSuite suite = TestSuite.create("my_suite") .test( "my_test", context -> { sameContext.set(checkTest(context)); count.compareAndSet(0, 1); }); TestReporter reporter = new TestReporter(); run(suite, reporter); reporter.await(); assertTrue(sameContext.get()); assertEquals(1, count.get()); assertEquals(0, reporter.exceptions.size()); assertEquals(1, reporter.results.size()); TestResult result = reporter.results.get(0); assertEquals("my_test", result.name()); assertTrue(result.succeeded()); assertFalse(result.failed()); assertNull(result.failure()); }
@Test public void runTestWithAsyncCompletionCompletedInTest() throws Exception { AtomicBoolean ok = new AtomicBoolean(); TestSuite suite = TestSuite.create("my_suite") .test( "my_test", context -> { Async async = context.async(); async.complete(); try { async.complete(); } catch (IllegalStateException ignore) { } ok.set(true); }); TestReporter reporter = new TestReporter(); run(suite, reporter); reporter.await(); assertTrue(ok.get()); assertTrue(reporter.completed()); assertEquals(0, reporter.exceptions.size()); assertEquals(1, reporter.results.size()); TestResult result = reporter.results.get(0); assertEquals("my_test", result.name()); assertTrue(result.succeeded()); assertFalse(result.failed()); assertNull(result.failure()); }