In SpringBoot3.0.3, it is mandatory to specify the Main class for the test.
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
This error occurred when I upgraded from 3.0.2 to 3.0.3. In 3.0.2, there was no such problem
In the update log, I did not get the place where the test has changed
Comment From: livk-cloud
I suspect that the change of
org.springframework.core.io.support.PathMatchingResourcePatternResolvermakes it impossible to search for class
Comment From: wilkinsona
Thanks for the report. Unfortunately, the sample does not reproduce the problem:
$ mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------< com.livk:spring-test-demo >----------------------
[INFO] Building spring-test-demo 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.3.0:resources (default-resources) @ spring-test-demo ---
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) @ spring-test-demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/awilkinson/Downloads/spring-test-demo/target/classes
[INFO]
[INFO] --- maven-resources-plugin:3.3.0:testResources (default-testResources) @ spring-test-demo ---
[INFO] skip non existing resourceDirectory /Users/awilkinson/Downloads/spring-test-demo/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.10.1:testCompile (default-testCompile) @ spring-test-demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/awilkinson/Downloads/spring-test-demo/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ spring-test-demo ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.livk.springtestdemo.SpringTestDemoApplicationTests
07:43:30.100 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [SpringTestDemoApplicationTests]: using SpringBootContextLoader
07:43:30.104 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [com.livk.springtestdemo.SpringTestDemoApplicationTests]: no resource found for suffixes {-context.xml, Context.groovy}.
07:43:30.105 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [com.livk.springtestdemo.SpringTestDemoApplicationTests]: SpringTestDemoApplicationTests does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
07:43:30.140 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using ContextCustomizers for test class [SpringTestDemoApplicationTests]: [ImportsContextCustomizer, ExcludeFilterContextCustomizer, DuplicateJsonObjectContextCustomizer, MockitoContextCustomizer, TestRestTemplateContextCustomizer, DisableObservabilityContextCustomizer, PropertyMappingContextCustomizer, Customizer]
07:43:30.221 [main] DEBUG org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider - Identified candidate component class: file [/Users/awilkinson/Downloads/spring-test-demo/target/classes/com/livk/springtestdemo/SpringTestDemoApplication.class]
07:43:30.223 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Found @SpringBootConfiguration com.livk.springtestdemo.SpringTestDemoApplication for test class com.livk.springtestdemo.SpringTestDemoApplicationTests
07:43:30.323 [main] DEBUG org.springframework.test.context.util.TestContextSpringFactoriesUtils - Skipping candidate TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener] due to a missing dependency. Specify custom TestExecutionListener classes or make the default TestExecutionListener classes and their required dependencies available. Offending class: [org/springframework/transaction/interceptor/TransactionAttributeSource]
07:43:30.326 [main] DEBUG org.springframework.test.context.util.TestContextSpringFactoriesUtils - Skipping candidate TestExecutionListener [org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener] due to a missing dependency. Specify custom TestExecutionListener classes or make the default TestExecutionListener classes and their required dependencies available. Offending class: [org/springframework/transaction/interceptor/TransactionAttribute]
07:43:30.329 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using TestExecutionListeners for test class [SpringTestDemoApplicationTests]: [ServletTestExecutionListener, DirtiesContextBeforeModesTestExecutionListener, ApplicationEventsTestExecutionListener, MockitoTestExecutionListener, DependencyInjectionTestExecutionListener, DirtiesContextTestExecutionListener, EventPublishingTestExecutionListener, ResetMocksTestExecutionListener, RestDocsTestExecutionListener, MockRestServiceServerResetTestExecutionListener, MockMvcPrintOnlyOnFailureTestExecutionListener, WebDriverTestExecutionListener, MockWebServiceServerTestExecutionListener]
07:43:30.330 [main] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - Before test class: class [SpringTestDemoApplicationTests], class annotated with @DirtiesContext [false] with mode [null]
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.0.3)
2023-02-24T07:43:30.617Z INFO 88715 --- [ main] c.l.s.SpringTestDemoApplicationTests : Starting SpringTestDemoApplicationTests using Java 17.0.6 with PID 88715 (started by awilkinson in /Users/awilkinson/Downloads/spring-test-demo)
2023-02-24T07:43:30.618Z INFO 88715 --- [ main] c.l.s.SpringTestDemoApplicationTests : No active profile set, falling back to 1 default profile: "default"
2023-02-24T07:43:31.746Z INFO 88715 --- [ main] o.s.b.t.m.w.SpringBootMockServletContext : Initializing Spring TestDispatcherServlet ''
2023-02-24T07:43:31.747Z INFO 88715 --- [ main] o.s.t.web.servlet.TestDispatcherServlet : Initializing Servlet ''
2023-02-24T07:43:31.749Z INFO 88715 --- [ main] o.s.t.web.servlet.TestDispatcherServlet : Completed initialization in 0 ms
2023-02-24T07:43:31.766Z INFO 88715 --- [ main] c.l.s.SpringTestDemoApplicationTests : Started SpringTestDemoApplicationTests in 1.402 seconds (process running for 2.365)
MockHttpServletRequest:
HTTP Method = GET
Request URI = /home
Parameters = {}
Headers = []
Body = null
Session Attrs = {}
Handler:
Type = com.livk.springtestdemo.SpringTestDemoApplication
Method = com.livk.springtestdemo.SpringTestDemoApplication#home()
Async:
Async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 200
Error message = null
Headers = [Content-Type:"text/plain;charset=UTF-8", Content-Length:"12"]
Content type = text/plain;charset=UTF-8
Body = Hello World!
Forwarded URL = null
Redirected URL = null
Cookies = []
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.342 s - in com.livk.springtestdemo.SpringTestDemoApplicationTests
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.398 s
[INFO] Finished at: 2023-02-24T07:43:32Z
[INFO] ------------------------------------------------------------------------
Comment From: livk-cloud
Thank you for your answer. There is no problem when I test on another computer. Maybe it is caused by some other unknown factors.
Comment From: wilkinsona
I think this is another symptom of the suspected regression in Spring Framework that's been identified in https://github.com/spring-projects/spring-boot/issues/34379. @livk-cloud, if I am right, you can avoid the problem by not having any spaces in your application's path.
Comment From: livk-cloud
I think this is another symptom of the suspected regression in Spring Framework that's been identified in #34379. @livk-cloud, if I am right, you can avoid the problem by not having any spaces in your application's path.
Yes, my path contains spaces, when I use
_instead of spaces, my program has no problem, maybe it has something to do with windows