From 2c4808760431dc8e26cdc31407626ce25783a0bd Mon Sep 17 00:00:00 2001 From: Danny Thomas Date: Thu, 15 Jun 2017 14:56:45 -0700 Subject: [PATCH 1/2] Ensure SpringBootDITEL fails with the original failure See gh-9534 --- ...otDependencyInjectionTestExecutionListener.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/SpringBootDependencyInjectionTestExecutionListener.java b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/SpringBootDependencyInjectionTestExecutionListener.java index 5b3fcbd0fc..90d186d325 100644 --- a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/SpringBootDependencyInjectionTestExecutionListener.java +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/SpringBootDependencyInjectionTestExecutionListener.java @@ -44,11 +44,15 @@ public class SpringBootDependencyInjectionTestExecutionListener super.prepareTestInstance(testContext); } catch (Exception ex) { - ApplicationContext context = testContext.getApplicationContext(); - if (context instanceof ConfigurableApplicationContext) { - ConditionEvaluationReport report = ConditionEvaluationReport - .get(((ConfigurableApplicationContext) context).getBeanFactory()); - System.err.println(new ConditionEvaluationReportMessage(report)); + try { + ApplicationContext context = testContext.getApplicationContext(); + if (context instanceof ConfigurableApplicationContext) { + ConditionEvaluationReport report = ConditionEvaluationReport + .get(((ConfigurableApplicationContext) context).getBeanFactory()); + System.err.println(new ConditionEvaluationReportMessage(report)); + } + } + catch (Exception ignore) { } throw ex; } From b7080ddec53e1dbd505f0d8c595bd64aaeb844e6 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 24 Jul 2017 11:37:40 +0100 Subject: [PATCH 2/2] Polish "Ensure SpringBootDITEL fails with the original failure" --- ...endencyInjectionTestExecutionListener.java | 27 +++++++++++-------- ...cyInjectionTestExecutionListenerTests.java | 17 ++++++++++++ 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/SpringBootDependencyInjectionTestExecutionListener.java b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/SpringBootDependencyInjectionTestExecutionListener.java index 90d186d325..2a616a2a95 100644 --- a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/SpringBootDependencyInjectionTestExecutionListener.java +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/SpringBootDependencyInjectionTestExecutionListener.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -44,20 +44,25 @@ public class SpringBootDependencyInjectionTestExecutionListener super.prepareTestInstance(testContext); } catch (Exception ex) { - try { - ApplicationContext context = testContext.getApplicationContext(); - if (context instanceof ConfigurableApplicationContext) { - ConditionEvaluationReport report = ConditionEvaluationReport - .get(((ConfigurableApplicationContext) context).getBeanFactory()); - System.err.println(new ConditionEvaluationReportMessage(report)); - } - } - catch (Exception ignore) { - } + outputConditionEvaluationReport(testContext); throw ex; } } + private void outputConditionEvaluationReport(TestContext testContext) { + try { + ApplicationContext context = testContext.getApplicationContext(); + if (context instanceof ConfigurableApplicationContext) { + ConditionEvaluationReport report = ConditionEvaluationReport + .get(((ConfigurableApplicationContext) context).getBeanFactory()); + System.err.println(new ConditionEvaluationReportMessage(report)); + } + } + catch (Exception ex) { + // Allow original failure to be reported + } + } + static class PostProcessor implements DefaultTestExecutionListenersPostProcessor { @Override diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/SpringBootDependencyInjectionTestExecutionListenerTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/SpringBootDependencyInjectionTestExecutionListenerTests.java index db0df23ced..408e58718d 100644 --- a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/SpringBootDependencyInjectionTestExecutionListenerTests.java +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/SpringBootDependencyInjectionTestExecutionListenerTests.java @@ -18,6 +18,7 @@ package org.springframework.boot.test.autoconfigure; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.ImportAutoConfiguration; @@ -30,6 +31,7 @@ import org.springframework.test.context.TestContext; import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; import static org.assertj.core.api.Assertions.assertThat; +import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.Matchers.containsString; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; @@ -44,6 +46,9 @@ public class SpringBootDependencyInjectionTestExecutionListenerTests { @Rule public OutputCapture out = new OutputCapture(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + private SpringBootDependencyInjectionTestExecutionListener reportListener = new SpringBootDependencyInjectionTestExecutionListener(); @Test @@ -73,6 +78,18 @@ public class SpringBootDependencyInjectionTestExecutionListenerTests { this.out.expect(containsString("Negative matches")); } + @Test + public void originalFailureIsThrownWhenReportGenerationFails() throws Exception { + TestContext testContext = mock(TestContext.class); + IllegalStateException originalFailure = new IllegalStateException(); + given(testContext.getTestInstance()).willThrow(originalFailure); + SpringApplication application = new SpringApplication(Config.class); + application.setWebEnvironment(false); + given(testContext.getApplicationContext()).willThrow(new RuntimeException()); + this.thrown.expect(is(originalFailure)); + this.reportListener.prepareTestInstance(testContext); + } + @Configuration @ImportAutoConfiguration(JacksonAutoConfiguration.class) static class Config {