Ensure test @PostConstructs are only called once
Rename AutoConfigureReportTestExecutionListener to SpringBootDependencyInjectionTestExecutionListener and ensure that it replaces any existing DependencyInjectionTestExecutionListener. Prior to this commit the registration of two DependencyInjection listeners would cause @PostConstruct methods on tests to be called twice. In order to allow the standard DependencyInjectionTestExecutionListener to be removed a new DefaultTestExecutionListenersPostProcessor interface has been introduced. Fixes gh-6874pull/6891/merge
parent
3d89dabb4b
commit
7134586310
@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright 2012-2016 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.test.autoconfigure;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link SpringBootDependencyInjectionTestExecutionListener}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class SpringBootDependencyInjectionTestExecutionListenerPostConstructIntegrationTests {
|
||||
|
||||
private List<String> calls = new ArrayList<String>();
|
||||
|
||||
@PostConstruct
|
||||
public void postConstruct() {
|
||||
StringWriter writer = new StringWriter();
|
||||
new RuntimeException().printStackTrace(new PrintWriter(writer));
|
||||
this.calls.add(writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void postConstructShouldBeInvokedOnlyOnce() throws Exception {
|
||||
// gh-6874
|
||||
assertThat(this.calls).hasSize(1);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class Config {
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright 2012-2016 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.test.context;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.test.context.TestExecutionListener;
|
||||
|
||||
/**
|
||||
* Callback interface trigger from {@link SpringBootTestContextBootstrapper} that can be
|
||||
* used to post-process the list of default {@link TestExecutionListener} classes to be
|
||||
* used by a test. Can be used to add or remove existing listener classes.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @since 1.4.1
|
||||
* @see SpringBootTest
|
||||
*/
|
||||
public interface DefaultTestExecutionListenersPostProcessor {
|
||||
|
||||
/**
|
||||
* Post process the list of default {@link TestExecutionListener} classes to be used.
|
||||
* @param listeners the source listeners
|
||||
* @return the actual listeners that should be used
|
||||
*/
|
||||
Set<Class<? extends TestExecutionListener>> postProcessDefaultTestExecutionListeners(
|
||||
Set<Class<? extends TestExecutionListener>> listeners);
|
||||
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright 2012-2016 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.test.context.bootstrap;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.boot.test.context.DefaultTestExecutionListenersPostProcessor;
|
||||
import org.springframework.test.context.TestContext;
|
||||
import org.springframework.test.context.TestExecutionListener;
|
||||
import org.springframework.test.context.support.AbstractTestExecutionListener;
|
||||
|
||||
/**
|
||||
* Test {@link DefaultTestExecutionListenersPostProcessor}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class TestDefaultTestExecutionListenersPostProcessor
|
||||
implements DefaultTestExecutionListenersPostProcessor {
|
||||
|
||||
@Override
|
||||
public Set<Class<? extends TestExecutionListener>> postProcessDefaultTestExecutionListeners(
|
||||
Set<Class<? extends TestExecutionListener>> listeners) {
|
||||
listeners.add(ExampleTestExecutionListener.class);
|
||||
return listeners;
|
||||
}
|
||||
|
||||
static class ExampleTestExecutionListener extends AbstractTestExecutionListener {
|
||||
|
||||
@Override
|
||||
public void prepareTestInstance(TestContext testContext) throws Exception {
|
||||
Object testInstance = testContext.getTestInstance();
|
||||
if (testInstance instanceof SpringBootTestContextBootstrapperTests) {
|
||||
SpringBootTestContextBootstrapperTests test = (SpringBootTestContextBootstrapperTests) testInstance;
|
||||
test.defaultTestExecutionListenersPostProcessorCalled = true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,2 @@
|
||||
org.springframework.boot.test.context.DefaultTestExecutionListenersPostProcessor=\
|
||||
org.springframework.boot.test.context.bootstrap.TestDefaultTestExecutionListenersPostProcessor
|
Loading…
Reference in New Issue