Polish "Ensure SpringBootDITEL fails with the original failure"

pull/9758/merge
Andy Wilkinson 7 years ago
parent 2c48087604
commit b7080ddec5

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -44,6 +44,12 @@ public class SpringBootDependencyInjectionTestExecutionListener
super.prepareTestInstance(testContext); super.prepareTestInstance(testContext);
} }
catch (Exception ex) { catch (Exception ex) {
outputConditionEvaluationReport(testContext);
throw ex;
}
}
private void outputConditionEvaluationReport(TestContext testContext) {
try { try {
ApplicationContext context = testContext.getApplicationContext(); ApplicationContext context = testContext.getApplicationContext();
if (context instanceof ConfigurableApplicationContext) { if (context instanceof ConfigurableApplicationContext) {
@ -52,9 +58,8 @@ public class SpringBootDependencyInjectionTestExecutionListener
System.err.println(new ConditionEvaluationReportMessage(report)); System.err.println(new ConditionEvaluationReportMessage(report));
} }
} }
catch (Exception ignore) { catch (Exception ex) {
} // Allow original failure to be reported
throw ex;
} }
} }

@ -18,6 +18,7 @@ package org.springframework.boot.test.autoconfigure;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
@ -30,6 +31,7 @@ import org.springframework.test.context.TestContext;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@ -44,6 +46,9 @@ public class SpringBootDependencyInjectionTestExecutionListenerTests {
@Rule @Rule
public OutputCapture out = new OutputCapture(); public OutputCapture out = new OutputCapture();
@Rule
public ExpectedException thrown = ExpectedException.none();
private SpringBootDependencyInjectionTestExecutionListener reportListener = new SpringBootDependencyInjectionTestExecutionListener(); private SpringBootDependencyInjectionTestExecutionListener reportListener = new SpringBootDependencyInjectionTestExecutionListener();
@Test @Test
@ -73,6 +78,18 @@ public class SpringBootDependencyInjectionTestExecutionListenerTests {
this.out.expect(containsString("Negative matches")); 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 @Configuration
@ImportAutoConfiguration(JacksonAutoConfiguration.class) @ImportAutoConfiguration(JacksonAutoConfiguration.class)
static class Config { static class Config {

Loading…
Cancel
Save