5

我目前在 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() {...}

    }
4

1 回答 1

0

您需要确保library模块pom.xml包含 -

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-test</artifactId>
    <version>1.5.2.RELEASE</version>
</dependency>

这是模块使用@SpringBootTest注释所必需的。您可能已经在您的app模块中使用相同但未包含在library模块中。


好吧发布编辑,发现问题可能与执行 JpaTest 时无法找到 @SpringBootConfiguration 重复

还从同一线程中引用托马斯的回答,here

和其他一些注解有关@DataJpaTest的是,它们@SpringBootConfiguration在当前包中查找注解,如果在那里找不到,则遍历包层次结构,直到找到为止。

例如,如果您的测试类的完全限定名称是 com.example.test.JpaTest,而您的应用程序的名称是 com.example.Application,那么您的测试类将能够找到@SpringBootApplication(以及其中的 @SpringBootConfiguration)。

但是,如果应用程序位于包层次结构的不同分支中,例如com.example.application.Application,它将找不到它。

对您来说似乎就是这种情况,您正在尝试在不同的模块本身中测试应用程序。因此,您看到的错误。

于 2017-03-05T10:09:09.277 回答