[英]Problems with Testing Spring Boot Application with Multi Maven Project Setup
我目前在 Spring Boot 和多 Maven 项目结构方面遇到了一些问题。 我正在使用 Spring Boot 4.3.1。
我的项目结构如下:
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)
application
模块依赖于library
。
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)
我的第一个猜测是library
需要依赖于application
,但这会导致循环。
有没有办法解决这个问题? 如何正确构建我的应用程序?
非常感谢您的建议。
MyLibraryTest
如下所示:
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class MyLibraryTest {
@Autowired
private MyService service;
@Test
public void testMyService_Save() {...}
}
您需要确保library
模块的pom.xml
包含 -
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
这是模块使用@SpringBootTest
注释所@SpringBootTest
。 您可能已经在app
模块中使用了相同的内容,但未包含在library
模块中。
好吧发布编辑,发现问题可能是在执行 JpaTest 时无法找到 @SpringBootConfiguration 的重复
还引用了同一线程中托马斯的回答,这里
关于
@DataJpaTest
和其他一些注解的事情是他们在当前包中寻找@SpringBootConfiguration
注解,如果他们在那里找不到它,他们就会遍历包层次结构直到找到它。例如,如果您的测试类的完全限定名称是
com.example.test.JpaTest
而您的应用程序的名称是com.example.Application
,那么您的测试类将能够找到@SpringBootApplication
(并且在其中,@SpringBootConfiguration
)。但是,如果应用程序驻留在包层次结构的不同分支中,例如
com.example.application.Application
,它将找不到它。
对您来说似乎就是这种情况,您试图在不同的模块本身中测试应用程序。 因此,您看到的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.