【发布时间】: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 -
stackoverflow.com/questions/39084491/… 在这种情况下会有所帮助。可能是这个本身的副本。
标签: java maven spring-boot dependencies spring-boot-test