use*_*695 5 java dependencies maven spring-boot spring-boot-test
我目前在 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)
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)
我的第一个猜测是library需要依赖application,但这会导致循环。
有没有办法解决这个问题?如何正确构建我的应用程序?
非常感谢您的建议。
MyLibraryTest 看起来如下:
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class MyLibraryTest {
@Autowired
private MyService service;
@Test
public void testMyService_Save() {...}
}
Run Code Online (Sandbox Code Playgroud)
您需要确保该library模块pom.xml包括 -
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
这是模块使用@SpringBootTest注释所必需的。您可能已经在模块中使用了相同的内容,app但未包含在library模块中。
好吧,发布编辑,发现问题可能与执行 JpaTest 时无法找到 @SpringBootConfiguration重复
还引用了托马斯在同一线程中的回答,这里
关于和其他一些注释的事情
@DataJpaTest是,它们在当前包中查找@SpringBootConfiguration注释,如果在那里找不到它,它们会遍历包层次结构,直到找到它。例如,如果您的测试类的完全限定名称是 ,
com.example.test.JpaTest而您的应用程序的完全限定名称是com.example.Application,那么您的测试类将能够找到@SpringBootApplication(以及其中的@SpringBootConfiguration)。但是,如果应用程序驻留在包层次结构的不同分支中,例如 ,则
com.example.application.Application它将找不到它。
这似乎适合您,您正在尝试在不同的模块本身中测试应用程序。因此您会看到错误。
| 归档时间: |
|
| 查看次数: |
2208 次 |
| 最近记录: |