Java 使用多Maven项目设置测试Spring Boot应用程序时出现的问题
我目前在springboot和multi-maven项目结构方面遇到了一些问题。我使用的是SpringBoot4.3.1 我的项目结构如下所示:Java 使用多Maven项目设置测试Spring Boot应用程序时出现的问题,java,maven,spring-boot,dependencies,spring-boot-test,Java,Maven,Spring Boot,Dependencies,Spring Boot Test,我目前在springboot和multi-maven项目结构方面遇到了一些问题。我使用的是SpringBoot4.3.1 我的项目结构如下所示: parent -- pom.xml -- application -- pom.xml -- src -- main -- java -- Application.java (annotated with @SpringBootApplication) -- test
parent
-- pom.xml
-- application
-- pom.xml
-- src
-- main
-- java
-- Application.java (annotated with @SpringBootApplication)
-- test
-- java
-- MyApplicationTest.java (annotated with @SpringBootTest)
-- library
-- pom.xml
-- src
-- main
-- java (...)
-- test
-- java
-- MyLibraryTest.java (annotated with @SpringBootTest)
应用程序
模块依赖于库
MyApplicationTest
工作得非常好,但是运行MyLibraryTest
会失败,出现以下错误:
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test at org.springframework.util.Assert.state(Assert.java:392)
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.getOr FindConfigurationClasses(SpringBootTestContextBootstrapper.java:173)
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.processMergedContextConfiguration(SpringBootTestContextBootstrapper.java:133)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:409)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:305)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildTestContext(AbstractTestContextBootstrapper.java:112)
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.buildTestContext(SpringBootTestContextBootstrapper.java:78)
at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:120)
at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:105)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:152)
您需要确保
库
模块的pom.xml
包括-
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
org.springframework.boot
也引用了托马斯在同一条线上的回答
关于@DataJpaTest
和其他一些注释的一点是
在当前包中查找@SpringBootConfiguration
注释,
如果在那里找不到,则遍历包层次结构
直到他们找到它
例如,如果测试类的完全限定名为
com.example.test.JpaTest
而您的应用程序的
com.example.Application
,则您的测试类将能够找到
@SpringBootApplication
(其中
@SpringBootConfiguration
)
如果应用程序位于包的不同分支中
然而,像com.example.application.application
这样的层次结构
没有找到它
对于您来说似乎就是这样,您正在尝试在不同的模块中测试应用程序。因此,您看到了错误。请共享查找错误的MyLibraryTest
的代码。此外,确保它在其pom中定义了所有必需的依赖项(如app->pom.xml中已定义的)@SpringBootTest
在这种情况下会有所帮助。可能是它本身的副本。@user3278695如前所述,请编辑问题并共享MyLIbraryTest
的代码以了解更多信息。您将如何解决此问题?
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>