/** * Invoke method after enrichment. Inexplicably, the {@code @Before} * and {@code @After} methods are not called when running this * executor. Calling them manually for now. * * @param testMethodExecutor Method executor. * @return Test result. */ public TestResult invoke(TestMethodExecutor testMethodExecutor) { RequestContextController controller = enricher.getRequestContextController(); try { controller.activate(); Object object = testMethodExecutor.getInstance(); Method method = testMethodExecutor.getMethod(); LOGGER.info("Invoking '" + method + "' on " + object); enricher.enrich(object); invokeAnnotated(object, Before.class); testMethodExecutor.invoke(enricher.resolve(method)); invokeAnnotated(object, After.class); } catch (Throwable t) { return TestResult.failed(t); } finally { controller.deactivate(); } return TestResult.passed(); }
private TestResult createFailedResult(Throwable throwable) { return TestResult.failed(throwable); } }
@Test public void should_mark_test_result_as_failed_if_there_are_only_failures() throws Exception { //given final TestResult e1 = failed(new RuntimeException("Exception 1")); final TestResult e2 = failed(new RuntimeException("Exception 2")); final List<TestResult> testResults = asList(e1, e2); // when final TestResult result = TestResult.flatten(testResults); // then assertThat(result.getStatus()).isEqualTo(TestResult.Status.FAILED); }
@Test public void should_create_exception_messages_in_order() throws Exception { // given final RuntimeException ex1 = new RuntimeException("Exception 1"); final TestResult e1 = failed(ex1); final RuntimeException ex2 = new RuntimeException("Exception 2"); final TestResult e2 = failed(ex2); final List<TestResult> testResults = asList(e2, e1); // when final TestResult result = TestResult.flatten(testResults); // then assertThat(result.getThrowable().getMessage()).isEqualTo("Exception 1: '[java.lang.RuntimeException] Exception 2'" + LINE_SEPARATOR + "Exception 2: '[java.lang.RuntimeException] Exception 1'" + LINE_SEPARATOR); }
@Test public void should_combine_exceptions_for_all_failures() throws Exception { // given final RuntimeException ex1 = new RuntimeException("Exception 1"); final TestResult e1 = failed(ex1); final RuntimeException ex2 = new RuntimeException("Exception 2"); final TestResult e2 = failed(ex2); final List<TestResult> testResults = asList(e1, e2); // when final TestResult result = TestResult.flatten(testResults); // then assertThat(result.getThrowable()).isInstanceOf(CombinedException.class); assertThat(((CombinedException) result.getThrowable()).getCauses()).containsExactly(ex1, ex2); }
public TestResult getTestResult() { if (context.getFailedConfigurations().size() > 0) { return TestResult.failed( context.getFailedConfigurations().getAllResults().iterator().next().getThrowable()); } else if (context.getFailedTests().size() > 0) { return TestResult.failed( context.getFailedTests().getAllResults().iterator().next().getThrowable()); } else if (context.getSkippedTests().size() > 0) { return TestResult.skipped().setThrowable(context.getSkippedTests().getAllResults().iterator().next().getThrowable()); } if (context.getPassedTests().size() > 0) { return TestResult.passed().setThrowable( context.getPassedTests().getAllResults().iterator().next().getThrowable()); } return TestResult.failed( new RuntimeException("Unknown test result: " + context).fillInStackTrace()); } }
@Test public void should_ignore_null_entries_in_the_list_of_test_results() throws Exception { // given final TestResult p1 = TestResult.passed("First test passed"); final TestResult f1 = failed(new RuntimeException("Exception")); final List<TestResult> testResults = asList(p1, null, f1); // when final TestResult result = TestResult.flatten(testResults); // then assertThat(result.getStatus()).isEqualTo(TestResult.Status.FAILED); }
@Test public void should_propagate_exception_when_only_one_cause_reported() throws Exception { // given final RuntimeException cause = new RuntimeException("Exception 1"); final TestResult f1 = failed(cause); final List<TestResult> testResults = asList(f1); // when final TestResult result = TestResult.flatten(testResults); // then assertThat(result.getThrowable()).isEqualTo(cause); }
@Test public void should_mark_test_result_as_failed_if_there_is_at_least_one_failure() throws Exception { // given final TestResult p1 = TestResult.passed("First test passed"); final TestResult s1 = TestResult.skipped("Not implemented yet"); final TestResult f1 = failed(new RuntimeException("Exception")); final List<TestResult> testResults = asList(p1, s1, f1); // when final TestResult result = TestResult.flatten(testResults); // then assertThat(result.getStatus()).isEqualTo(TestResult.Status.FAILED); }
@Test public void shouldPassResultIfNoExceptionCaughtAfterJunit() throws Exception { TestResult result = TestResult.failed(new NullPointerException()); new UpdateTestResultBeforeAfter().update(event, result); Assert.assertEquals(Status.PASSED, result.getStatus()); Assert.assertNull(result.getThrowable()); }
@Test public void shouldPassResultIfNoExceptionCaughtAfterJunit() throws Exception { TestResult result = TestResult.failed(new NullPointerException()); new UpdateTestResultBeforeAfter().update(event, result); Assert.assertEquals(Status.PASSED, result.getStatus()); Assert.assertNull(result.getThrowable()); }
@Override public TestResult execute(final Class<?> testClass, final String methodName) { final Result testResult = new Result(); try { final Sputnik spockRunner = new ArquillianSputnik(testClass); spockRunner.filter(new SpockSpecificationFilter(spockRunner, methodName)); runTest(spockRunner, testResult); } catch (Exception e) { return TestResult.failed(e); } return convertToTestResult(testResult); }
@Test public void shouldFailResultIfExceptionCaughtAfterJunit() throws Exception { State.caughtExceptionAfterJunit(new AssertionError("A")); TestResult result = TestResult.failed(new NullPointerException("A")); new UpdateTestResultBeforeAfter().update(event, result); State.caughtExceptionAfterJunit(null); Assert.assertEquals(Status.FAILED, result.getStatus()); Assert.assertNotNull(result.getThrowable()); Assert.assertTrue(result.getThrowable() instanceof AssertionError); } }
@Test public void shouldFailResultIfExceptionCaughtAfterJunit() throws Exception { State.caughtExceptionAfterJunit(new AssertionError("A")); TestResult result = TestResult.failed(new NullPointerException("A")); new UpdateTestResultBeforeAfter().update(event, result); State.caughtExceptionAfterJunit(null); Assert.assertEquals(Status.FAILED, result.getStatus()); Assert.assertNotNull(result.getThrowable()); Assert.assertTrue(result.getThrowable() instanceof AssertionError); } }
public void execute(@Observes Test event) throws Exception { TestResult result = TestResult.passed(); try { event.getTestMethodExecutor().invoke( enrichArguments( event.getTestMethod(), serviceLoader.get().all(TestEnricher.class))); } catch (Throwable e) { result = TestResult.failed(e); } finally { result.setEnd(System.currentTimeMillis()); } testResult.set(result); }
public TestResult execute(Class<?> testClass, String methodName) { TestListener resultListener = new TestListener(); TestNG runner = new TestNG(false); runner.setVerbose(0); runner.addListener(resultListener); runner.addListener(new RemoveDependsOnTransformer()); runner.setXmlSuites( Collections.singletonList(createSuite(testClass, methodName))); //we catch problems in executing run method, e.g. ClassDefNotFoundError and wrap it in TestResult try { runner.run(); } catch (Throwable ex) { return TestResult.failed(ex); } return resultListener.getTestResult(); }
public TestResult execute(Class<?> testClass, String methodName) { TestListener resultListener = new TestListener(); TestNG runner = new TestNG(false); runner.setVerbose(0); runner.addListener(resultListener); runner.addListener(new RemoveDependsOnTransformer()); runner.setXmlSuites( Collections.singletonList(createSuite(testClass, methodName))); //we catch problems in executing run method, e.g. ClassDefNotFoundError and wrap it in TestResult try { runner.run(); } catch (Throwable ex) { return TestResult.failed(ex); } return resultListener.getTestResult(); }
@Test public void shouldSkipResultIfExceptionCaughtAfterJunit() throws Exception { State.caughtExceptionAfterJunit(new AssumptionViolatedException("A")); TestResult result = TestResult.failed(new AssumptionViolatedException("A")); new UpdateTestResultBeforeAfter().update(event, result); State.caughtExceptionAfterJunit(null); Assert.assertEquals(Status.SKIPPED, result.getStatus()); Assert.assertNotNull(result.getThrowable()); Assert.assertTrue(result.getThrowable() instanceof AssumptionViolatedException); }
@Test public void shouldSkipResultIfExceptionCaughtAfterJunit() throws Exception { State.caughtExceptionAfterJunit(new AssumptionViolatedException("A")); TestResult result = TestResult.failed(new AssumptionViolatedException("A")); new UpdateTestResultBeforeAfter().update(event, result); State.caughtExceptionAfterJunit(null); Assert.assertEquals(Status.SKIPPED, result.getStatus()); Assert.assertNotNull(result.getThrowable()); Assert.assertTrue(result.getThrowable() instanceof AssumptionViolatedException); }
@Test public void shouldReturnThrownException() throws Exception { MockTestRunner.add(TestResult.failed(new Exception().fillInStackTrace())); ServletMethodExecutor executor = createExecutor(); TestResult result = executor.invoke(new MockTestExecutor()); Assert.assertEquals( "Should have returned a passed test", MockTestRunner.wantedResults.getStatus(), result.getStatus()); Assert.assertNotNull( "Exception should have been thrown", result.getThrowable()); }