Skip to content

SpringBootTest.UseMainMethod.WHEN_AVAILABLE and ALWAYS are incompatible with package-private or parameter-less main method #48271

@kzander91

Description

@kzander91

Spring Boot: 3.4.12, 3.5.8, 4.0.0

Both @SpringBootTest(useMainMethod = ALWAYS) and @SpringBootTest(useMainMethod = WHEN_AVAILABLE) fail with the following exception, when the main method is package-private:

java.lang.IllegalStateException: Could not access method or field: class org.springframework.util.ReflectionUtils cannot access a member of class com.example.demo.Demo12Application with package access
        at org.springframework.util.ReflectionUtils.handleReflectionException(ReflectionUtils.java:109)
        at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:284)
        at org.springframework.boot.test.context.SpringBootContextLoader.lambda$loadContext$1(SpringBootContextLoader.java:143)
        at org.springframework.boot.test.context.SpringBootContextLoader$ContextLoaderHook.lambda$runMain$0(SpringBootContextLoader.java:566)
        at org.springframework.util.function.ThrowingSupplier.get(ThrowingSupplier.java:58)
        at org.springframework.util.function.ThrowingSupplier.get(ThrowingSupplier.java:46)
        at org.springframework.boot.SpringApplication.withHook(SpringApplication.java:1474)
        at org.springframework.boot.test.context.SpringBootContextLoader$ContextLoaderHook.run(SpringBootContextLoader.java:573)
        at org.springframework.boot.test.context.SpringBootContextLoader$ContextLoaderHook.runMain(SpringBootContextLoader.java:565)
        at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:143)
        at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:114)
        at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:247)
        at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.lambda$loadContext$0(DefaultCacheAwareContextLoaderDelegate.java:167)

And with the following exception, when the main method is public but does not have parameters:

java.lang.IllegalStateException: Main method not found on 'com.example.demo.Demo12Application'
        at org.springframework.util.Assert.state(Assert.java:102)
        at org.springframework.boot.test.context.SpringBootContextLoader.getMainMethod(SpringBootContextLoader.java:173)
        at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:136)
        at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:114)
        at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:247)
        at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.lambda$loadContext$0(DefaultCacheAwareContextLoaderDelegate.java:167)

Reproducer: demo12.zip

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions