使用多 Maven 项目设置测试 Spring Boot 应用程序的问题
Posted
技术标签:
【中文标题】使用多 Maven 项目设置测试 Spring Boot 应用程序的问题【英文标题】:Problems with Testing Spring Boot Application with Multi Maven Project Setup 【发布时间】:2017-07-25 05:14:50 【问题描述】:我目前在 spring boot 和 multi 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() ...
【问题讨论】:
请分享您发现错误的MyLibraryTest
的代码。此外,请确保它在其 pom 中定义了所有必需的依赖项。(如已在应用程序 -> pom.xml 中定义)为@SpringBootTest
***.com/questions/39084491/… 在这种情况下会有所帮助。可能是这个本身的副本。
【参考方案1】:
您需要确保library
模块的pom.xml
包含-
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
这是模块使用@SpringBootTest
注解所必需的。您可能已经在 app
模块中使用了相同的内容,但未包含在 library
模块中。
好吧发布编辑,发现问题可能与Unable to find a @SpringBootConfiguration when doing a JpaTest重复
还引用了同一线程中 Thomas 的回答,here
关于
@DataJpaTest
和其他一些注释的事情是,它们 在当前包中寻找@SpringBootConfiguration
注解, 如果他们在那里找不到它,他们会遍历包层次结构 直到他们找到为止。例如,如果您的测试类的完全限定名称是
com.example.test.JpaTest
而你的应用程序是com.example.Application
,那么你的测试类就能找到@SpringBootApplication
(其中,@SpringBootConfiguration
)。如果应用程序驻留在包的不同分支中 然而,层次结构,如
com.example.application.Application
,它会 没找到。
对您来说似乎就是这种情况,您尝试在不同的模块本身中测试应用程序。因此,您会看到错误。
【讨论】:
@user3278695 如前所述,请编辑问题并分享MyLIbraryTest
的代码以进一步了解
那么你将如何解决这个问题?以上是关于使用多 Maven 项目设置测试 Spring Boot 应用程序的问题的主要内容,如果未能解决你的问题,请参考以下文章