diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc index 701d6cb959..d2c06e88fd 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc @@ -153,25 +153,17 @@ For example, here is an application that changes the banner mode and sets additi include::code:custom/MyApplication[] -Since customizations in the `main` method can affect the resulting `ApplicationContext`, Spring Boot will also attempt to use the `main` method for tests. -By default, `@SpringBootTest` will detect any `main` method on your `@SpringBootConfiguration` and run it in order to capture the `ApplicationContext`. -If your `@SpringBootConfiguration` class doesn't have a main method, the class itself is used directly to create the `ApplicationContext`. +Since customizations in the `main` method can affect the resulting `ApplicationContext`, it's possible that you might also want to use the `main` method to create the `ApplicationContext` used in your tests. +By default, `@SpringBootTest` will not call your `main` method, and instead the class itself is used directly to create the `ApplicationContext` -In some situations, you may find that you can't or don't want to run the `main` method in your tests. -If that's the case, you can change the `useMainMethod` attribute of `@SpringBootTest` to `UseMainMethod.NEVER`. +If you want to change this behavior, you can change the `useMainMethod` attribute of `@SpringBootTest` to `UseMainMethod.ALWAYS` or `UseMainMethod.WHEN_AVAILABLE`. +When set to `ALWAYS`, the test will fail if no `main` method can be found. +When set to `WHEN_AVAILABLE` the `main` method will be used if it is available, otherwise the standard loading mechanism will be used. -For example, you might have the following application class: +For example, the following test will invoke the `main` method of `MyApplication` in order to create the `ApplicationContext`. +If the main method sets additional profiles then those will be active when the `ApplicationContext` starts. -include::code:never/MyApplication[] - -If a test wants to use the `MyApplication` configuration without calling the main method, it can be written as follows: - -include::code:never/MyApplicationTests[] - -The test above will still use `MyApplication` to create the `ApplicationContext`, however, it won't call `MyCode.expensiveOperation()` since the `main` method is not invoked. - -If you want to do the opposite, and ensure that the `main` method is always invoked, you can set the `useMainMethod` attribute of `@SpringBootTest` to `UseMainMethod.ALWAYS`. -If this property is set, and no `main` method can be found the test will fail. +include::code:always/MyApplicationTests[] diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/usingmain/never/MyApplicationTests.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/usingmain/always/MyApplicationTests.java similarity index 91% rename from spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/usingmain/never/MyApplicationTests.java rename to spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/usingmain/always/MyApplicationTests.java index e16c5546c0..3b90ddea0a 100644 --- a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/usingmain/never/MyApplicationTests.java +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/usingmain/always/MyApplicationTests.java @@ -14,14 +14,14 @@ * limitations under the License. */ -package org.springframework.boot.docs.features.testing.springbootapplications.usingmain.never; +package org.springframework.boot.docs.features.testing.springbootapplications.usingmain.always; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.UseMainMethod; -@SpringBootTest(useMainMethod = UseMainMethod.NEVER) +@SpringBootTest(useMainMethod = UseMainMethod.ALWAYS) public class MyApplicationTests { @Test diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/usingmain/never/MyApplication.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/usingmain/never/MyApplication.java deleted file mode 100644 index 330e363921..0000000000 --- a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/usingmain/never/MyApplication.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright 2012-2022 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 - * - * https://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.docs.features.testing.springbootapplications.usingmain.never; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; - -@SpringBootApplication -public class MyApplication { - - public static void main(String[] args) { - MyCode.expensiveOperation(); - SpringApplication.run(MyApplication.class, args); - } - -} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/usingmain/never/MyCode.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/usingmain/never/MyCode.java deleted file mode 100644 index 87dbeb4dd7..0000000000 --- a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/usingmain/never/MyCode.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright 2012-2022 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 - * - * https://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.docs.features.testing.springbootapplications.usingmain.never; - -public final class MyCode { - - private MyCode() { - } - - static void expensiveOperation() { - } - -} diff --git a/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testing/springbootapplications/usingmain/never/MyApplicationTests.kt b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testing/springbootapplications/usingmain/always/MyApplicationTests.kt similarity index 91% rename from spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testing/springbootapplications/usingmain/never/MyApplicationTests.kt rename to spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testing/springbootapplications/usingmain/always/MyApplicationTests.kt index d6d27947f2..40bbe18104 100644 --- a/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testing/springbootapplications/usingmain/never/MyApplicationTests.kt +++ b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testing/springbootapplications/usingmain/always/MyApplicationTests.kt @@ -14,14 +14,14 @@ * limitations under the License. */ -package org.springframework.boot.docs.features.testing.springbootapplications.usingmain.custom.never +package org.springframework.boot.docs.features.testing.springbootapplications.usingmain.custom.always import org.junit.jupiter.api.Test import org.springframework.boot.test.context.SpringBootTest import org.springframework.boot.test.context.SpringBootTest.UseMainMethod import org.springframework.context.annotation.Import -@SpringBootTest(useMainMethod = UseMainMethod.NEVER) +@SpringBootTest(useMainMethod = UseMainMethod.ALWAYS) class MyApplicationTests { @Test diff --git a/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testing/springbootapplications/usingmain/never/MyApplication.kt b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testing/springbootapplications/usingmain/never/MyApplication.kt deleted file mode 100644 index 346371f62e..0000000000 --- a/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testing/springbootapplications/usingmain/never/MyApplication.kt +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 2012-2022 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 - * - * https://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.docs.features.testing.springbootapplications.usingmain.custom.never - -import org.springframework.boot.autoconfigure.SpringBootApplication -import org.springframework.boot.docs.using.structuringyourcode.locatingthemainclass.MyApplication -import org.springframework.boot.runApplication - -@SpringBootApplication -class MyApplication - -fun main(args: Array) { - MyCode.expensiveOperation() - runApplication(*args) -} diff --git a/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testing/springbootapplications/usingmain/never/MyCode.kt b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testing/springbootapplications/usingmain/never/MyCode.kt deleted file mode 100644 index 33b1de3af1..0000000000 --- a/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testing/springbootapplications/usingmain/never/MyCode.kt +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright 2012-2022 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 - * - * https://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.docs.features.testing.springbootapplications.usingmain.custom.never - -class MyCode { - companion object { - fun expensiveOperation() { - } - } -} diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTest.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTest.java index 70bb75535f..06e1ddfdef 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTest.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTest.java @@ -131,7 +131,7 @@ public @interface SpringBootTest { * @return the type of main method usage * @since 3.0.0 */ - UseMainMethod useMainMethod() default UseMainMethod.WHEN_AVAILABLE; + UseMainMethod useMainMethod() default UseMainMethod.NEVER; /** * An enumeration web environment modes. diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTestAnnotation.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTestAnnotation.java index 26a97984e8..978d4ee863 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTestAnnotation.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTestAnnotation.java @@ -55,7 +55,7 @@ class SpringBootTestAnnotation implements ContextCustomizer { private SpringBootTestAnnotation(SpringBootTest annotation) { this.args = (annotation != null) ? annotation.args() : NO_ARGS; this.webEnvironment = (annotation != null) ? annotation.webEnvironment() : WebEnvironment.NONE; - this.useMainMethod = (annotation != null) ? annotation.useMainMethod() : UseMainMethod.WHEN_AVAILABLE; + this.useMainMethod = (annotation != null) ? annotation.useMainMethod() : UseMainMethod.NEVER; } @Override diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-data-ldap/src/test/java/smoketest/data/ldap/SampleLdapApplicationTests.java b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-data-ldap/src/test/java/smoketest/data/ldap/SampleLdapApplicationTests.java index 5705f26d26..98d8409326 100644 --- a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-data-ldap/src/test/java/smoketest/data/ldap/SampleLdapApplicationTests.java +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-data-ldap/src/test/java/smoketest/data/ldap/SampleLdapApplicationTests.java @@ -20,7 +20,6 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.context.SpringBootTest.UseMainMethod; import org.springframework.boot.test.system.CapturedOutput; import org.springframework.boot.test.system.OutputCaptureExtension; @@ -32,7 +31,7 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Phillip Webb */ @ExtendWith(OutputCaptureExtension.class) -@SpringBootTest(useMainMethod = UseMainMethod.NEVER) +@SpringBootTest class SampleLdapApplicationTests { @Test diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-profile/src/test/java/smoketest/profile/ActiveProfilesTests.java b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-profile/src/test/java/smoketest/profile/ActiveProfilesTests.java index b60fb5de67..e122c493e8 100644 --- a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-profile/src/test/java/smoketest/profile/ActiveProfilesTests.java +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-profile/src/test/java/smoketest/profile/ActiveProfilesTests.java @@ -21,7 +21,6 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.env.EnvironmentPostProcessor; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.context.SpringBootTest.UseMainMethod; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.core.env.Environment; import org.springframework.test.context.ActiveProfiles; @@ -34,8 +33,7 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Madhura Bhave */ -@SpringBootTest(useMainMethod = UseMainMethod.NEVER, webEnvironment = WebEnvironment.NONE, - properties = { "enableEnvironmentPostProcessor=true" }) // gh-28530 +@SpringBootTest(webEnvironment = WebEnvironment.NONE, properties = { "enableEnvironmentPostProcessor=true" }) // gh-28530 @ActiveProfiles("hello") class ActiveProfilesTests { diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-profile/src/test/java/smoketest/profile/AttributeInjectionTests.java b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-profile/src/test/java/smoketest/profile/AttributeInjectionTests.java index aafb628ec3..42a26eefee 100644 --- a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-profile/src/test/java/smoketest/profile/AttributeInjectionTests.java +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-profile/src/test/java/smoketest/profile/AttributeInjectionTests.java @@ -20,12 +20,11 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.context.SpringBootTest.UseMainMethod; import static org.assertj.core.api.Assertions.assertThat; // gh-29169 -@SpringBootTest(useMainMethod = UseMainMethod.NEVER, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) class AttributeInjectionTests { @Autowired(required = false)