Spring Boot使用Maven从单独的jar运行可重用的集成测试,无法找到@SpringBootConfiguration

问题描述

我在名为“ lemon-auth-tests”的maven模块中进行了一些可重用的集成测试。 该模块包括Junit5测试和一个TestAdapter接口,该接口需要由其他使用这些测试的Maven模块来实现。 lemon-auth-tests也有一个AbstractMvcTest类,该类带有@SpringBootTest注释。 lemon-auth-tests的每个测试都从该基类扩展。 Maven模块Lemon-demo通过以下方式包含Lemon-auth-tests jar:

...
<dependency>
    <groupId>com.github.vincemann.spring-rapid</groupId>
    <artifactId>lemon-auth-tests</artifactId>
    <version>${project.version}</version>
    <scope>test</scope>
</dependency>
...
<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>


            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <dependenciesToScan>
                        <dependency>com.github.vincemann.spring-rapid:lemon-auth-tests</dependency>
                    </dependenciesToScan>
                </configuration>
            </plugin>

        </plugins>


    </build>

当我使用mvn test在Lemon-demo模块中运行测试时,找到并运行了该测试,但是Spring无法找到@SpringBootConfiguration。 对于Lemon-auth-tests中的每个测试类,它都会显示以下错误消息:

2020-10-09 07:46:23.218  INFO   --- [           main] .b.t.c.SpringBootTestContextBootstrapper : Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.github.vincemann.springlemon.authtests.ForgotPasswordMvcTests],using SpringBootContextLoader
2020-10-09 07:46:23.219  INFO   --- [           main] o.s.t.c.support.AbstractContextLoader    : Could not detect default resource locations for test class [com.github.vincemann.springlemon.authtests.ForgotPasswordMvcTests]: no resource found for suffixes {-context.xml,Context.groovy}.
2020-10-09 07:46:23.219  INFO   --- [           main] t.c.s.AnnotationConfigContextLoaderUtils : Could not detect default configuration classes for test class [com.github.vincemann.springlemon.authtests.ForgotPasswordMvcTests]: ForgotPasswordMvcTests does not declare any static,non-private,non-final,nested classes annotated with @Configuration.
2020-10-09 07:46:23.248  INFO   --- [           main] .b.t.c.SpringBootTestContextBootstrapper : Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.github.vincemann.springlemon.authtests.ForgotPasswordMvcTests],using SpringBootContextLoader
2020-10-09 07:46:23.249  INFO   --- [           main] o.s.t.c.support.AbstractContextLoader    : Could not detect default resource locations for test class [com.github.vincemann.springlemon.authtests.ForgotPasswordMvcTests]: no resource found for suffixes {-context.xml,Context.groovy}.
2020-10-09 07:46:23.249  INFO   --- [           main] t.c.s.AnnotationConfigContextLoaderUtils : Could not detect default configuration classes for test class [com.github.vincemann.springlemon.authtests.ForgotPasswordMvcTests]: ForgotPasswordMvcTests does not declare any static,nested classes annotated with @Configuration.
2020-10-09 07:46:23.273  INFO   --- [           main] .b.t.c.SpringBootTestContextBootstrapper : Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.github.vincemann.springlemon.authtests.ForgotPasswordMvcTests],using SpringBootContextLoader
2020-10-09 07:46:23.276  INFO   --- [           main] o.s.t.c.support.AbstractContextLoader    : Could not detect default resource locations for test class [com.github.vincemann.springlemon.authtests.ForgotPasswordMvcTests]: no resource found for suffixes {-context.xml,Context.groovy}.
2020-10-09 07:46:23.279  INFO   --- [           main] t.c.s.AnnotationConfigContextLoaderUtils : Could not detect default configuration classes for test class [com.github.vincemann.springlemon.authtests.ForgotPasswordMvcTests]: ForgotPasswordMvcTests does not declare any static,nested classes annotated with @Configuration.
[ERROR] Tests run: 1,Failures: 0,Errors: 1,Skipped: 0,Time elapsed: 0.097 s <<< FAILURE! - in com.github.vincemann.springlemon.authtests.ForgotPasswordMvcTests
[ERROR] com.github.vincemann.springlemon.authtests.ForgotPasswordMvcTests  Time elapsed: 0.097 s  <<< ERROR!
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration,you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

我假设Springs Test-Framework通过扫描lemon-auth-tests的类路径来查找@SpringBootConfiguration(即@SpringBootApplication中的i.E.),因此找不到任何内容。 我怎样才能告诉Spring使用我的Lemon-demo模块中的LemonDemoApplication.class(用@SpringBootApplication注释)? 我的想法是以某种方式以编程方式包括一个方法:在我的TestAdapter中,然后在AbstractMvcTest中,调用SpringBootTest.setApplicationClass(testAdapter.getApplicationClass())或类似的方法,而不是使用@SpringBootTest;尽管我认为必须有一个更优雅的解决方案。 完整的代码是here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...