From 7ab587a067254ea36b82dd7d0fde9b1ad5914e5e Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 12 Feb 2018 14:53:01 -0800 Subject: [PATCH] Add initializer support Add `ApplicationContextInitializer` support to `ApplicationContextRunner`. --- .../AbstractApplicationContextRunner.java | 54 +++++++++++++------ .../runner/ApplicationContextRunner.java | 14 +++-- .../ReactiveWebApplicationContextRunner.java | 11 ++-- .../runner/WebApplicationContextRunner.java | 14 +++-- ...AbstractApplicationContextRunnerTests.java | 11 +++- 5 files changed, 74 insertions(+), 30 deletions(-) diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunner.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunner.java index cf9e752d00..567960a516 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunner.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunner.java @@ -29,6 +29,7 @@ import org.springframework.boot.test.context.assertj.ApplicationContextAssert; import org.springframework.boot.test.context.assertj.ApplicationContextAssertProvider; import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.AnnotationConfigRegistry; import org.springframework.core.ResolvableType; @@ -99,6 +100,8 @@ public abstract class AbstractApplicationContextRunner contextFactory; + private final List> initializers; + private final TestPropertyValues environmentProperties; private final TestPropertyValues systemProperties; @@ -114,13 +117,14 @@ public abstract class AbstractApplicationContextRunner contextFactory) { - this(contextFactory, TestPropertyValues.empty(), TestPropertyValues.empty(), null, - null, Collections.emptyList()); + this(contextFactory, Collections.emptyList(), TestPropertyValues.empty(), + TestPropertyValues.empty(), null, null, Collections.emptyList()); } /** * Create a new {@link AbstractApplicationContextRunner} instance. * @param contextFactory the factory used to create the actual context + * @param initializers the initializers * @param environmentProperties the environment properties * @param systemProperties the system properties * @param classLoader the class loader @@ -128,6 +132,7 @@ public abstract class AbstractApplicationContextRunner contextFactory, + List> initializers, TestPropertyValues environmentProperties, TestPropertyValues systemProperties, ClassLoader classLoader, ApplicationContext parent, List configurations) { @@ -135,7 +140,9 @@ public abstract class AbstractApplicationContextRunner initializer) { + Assert.notNull(initializer, "Initializer must not be null"); + return newInstance(this.contextFactory, add(this.initializers, initializer), + this.environmentProperties, this.systemProperties, this.classLoader, + this.parent, this.configurations); + } + /** * Add the specified {@link Environment} property pairs. Key-value pairs can be * specified with colon (":") or equals ("=") separators. Override matching keys that @@ -154,9 +174,9 @@ public abstract class AbstractApplicationContextRunner contextFactory, + List> initializers, TestPropertyValues environmentProperties, TestPropertyValues systemProperties, ClassLoader classLoader, ApplicationContext parent, List configurations); @@ -295,6 +318,7 @@ public abstract class AbstractApplicationContextRunner 0) { ((AnnotationConfigRegistry) context).register(classes); } + this.initializers.forEach((initializer) -> initializer.initialize(context)); context.refresh(); } diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/ApplicationContextRunner.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/ApplicationContextRunner.java index 4b040e1ee8..a1b393c561 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/ApplicationContextRunner.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/ApplicationContextRunner.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. @@ -23,6 +23,7 @@ import org.springframework.boot.context.annotation.Configurations; import org.springframework.boot.test.context.assertj.AssertableApplicationContext; import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; @@ -60,21 +61,24 @@ public class ApplicationContextRunner extends private ApplicationContextRunner( Supplier contextFactory, + List> initializers, TestPropertyValues environmentProperties, TestPropertyValues systemProperties, ClassLoader classLoader, ApplicationContext parent, List configurations) { - super(contextFactory, environmentProperties, systemProperties, classLoader, - parent, configurations); + super(contextFactory, initializers, environmentProperties, systemProperties, + classLoader, parent, configurations); } @Override protected ApplicationContextRunner newInstance( Supplier contextFactory, + List> initializers, TestPropertyValues environmentProperties, TestPropertyValues systemProperties, ClassLoader classLoader, ApplicationContext parent, List configurations) { - return new ApplicationContextRunner(contextFactory, environmentProperties, - systemProperties, classLoader, parent, configurations); + return new ApplicationContextRunner(contextFactory, initializers, + environmentProperties, systemProperties, classLoader, parent, + configurations); } } diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/ReactiveWebApplicationContextRunner.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/ReactiveWebApplicationContextRunner.java index c1fea9cd27..2df945cc3a 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/ReactiveWebApplicationContextRunner.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/ReactiveWebApplicationContextRunner.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. @@ -25,6 +25,7 @@ import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebApplicationContext; import org.springframework.boot.web.reactive.context.ConfigurableReactiveWebApplicationContext; import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextInitializer; /** * An {@link AbstractApplicationContextRunner ApplicationContext runner} for a @@ -60,20 +61,22 @@ public final class ReactiveWebApplicationContextRunner extends private ReactiveWebApplicationContextRunner( Supplier contextFactory, + List> initializers, TestPropertyValues environmentProperties, TestPropertyValues systemProperties, ClassLoader classLoader, ApplicationContext parent, List configurations) { - super(contextFactory, environmentProperties, systemProperties, classLoader, - parent, configurations); + super(contextFactory, initializers, environmentProperties, systemProperties, + classLoader, parent, configurations); } @Override protected ReactiveWebApplicationContextRunner newInstance( Supplier contextFactory, + List> initializers, TestPropertyValues environmentProperties, TestPropertyValues systemProperties, ClassLoader classLoader, ApplicationContext parent, List configurations) { - return new ReactiveWebApplicationContextRunner(contextFactory, + return new ReactiveWebApplicationContextRunner(contextFactory, initializers, environmentProperties, systemProperties, classLoader, parent, configurations); } diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/WebApplicationContextRunner.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/WebApplicationContextRunner.java index fd9270b4ae..e104390f86 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/WebApplicationContextRunner.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/WebApplicationContextRunner.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. @@ -23,6 +23,7 @@ import org.springframework.boot.context.annotation.Configurations; import org.springframework.boot.test.context.assertj.AssertableWebApplicationContext; import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextInitializer; import org.springframework.mock.web.MockServletContext; import org.springframework.web.context.ConfigurableWebApplicationContext; import org.springframework.web.context.WebApplicationContext; @@ -64,21 +65,24 @@ public final class WebApplicationContextRunner extends private WebApplicationContextRunner( Supplier contextFactory, + List> initializers, TestPropertyValues environmentProperties, TestPropertyValues systemProperties, ClassLoader classLoader, ApplicationContext parent, List configurations) { - super(contextFactory, environmentProperties, systemProperties, classLoader, - parent, configurations); + super(contextFactory, initializers, environmentProperties, systemProperties, + classLoader, parent, configurations); } @Override protected WebApplicationContextRunner newInstance( Supplier contextFactory, + List> initializers, TestPropertyValues environmentProperties, TestPropertyValues systemProperties, ClassLoader classLoader, ApplicationContext parent, List configurations) { - return new WebApplicationContextRunner(contextFactory, environmentProperties, - systemProperties, classLoader, parent, configurations); + return new WebApplicationContextRunner(contextFactory, initializers, + environmentProperties, systemProperties, classLoader, parent, + configurations); } /** diff --git a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunnerTests.java b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunnerTests.java index fe5619321e..ee6ef51d5a 100644 --- a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunnerTests.java +++ b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunnerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. @@ -18,6 +18,7 @@ package org.springframework.boot.test.context.runner; import java.io.IOException; import java.util.UUID; +import java.util.concurrent.atomic.AtomicBoolean; import com.google.gson.Gson; import org.junit.Rule; @@ -50,6 +51,14 @@ public abstract class AbstractApplicationContextRunnerTests called.set(true)).run((context) -> { + }); + assertThat(called).isTrue(); + } + @Test public void runWithSystemPropertiesShouldSetAndRemoveProperties() { String key = "test." + UUID.randomUUID();