简体   繁体   English

使用多 Maven 项目设置测试 Spring Boot 应用程序的问题

[英]Problems with Testing Spring Boot Application with Multi Maven Project Setup

I am currently running into some problems with spring boot and multi maven project structure.我目前在 Spring Boot 和多 Maven 项目结构方面遇到了一些问题。 I am using Spring Boot 4.3.1.我正在使用 Spring Boot 4.3.1。

My project structure looks as follows:我的项目结构如下:

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 module has a dependency on library . application模块依赖于library

MyApplicationTest works perfectly fine, but running MyLibraryTest instead, I fail with the following error: 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)

My first guess is that library needs a dependency on application , but this causes a cycle.我的第一个猜测是library需要依赖于application ,但这会导致循环。

Is there any solution to that problem?有没有办法解决这个问题? How can I structure my application correctly?如何正确构建我的应用程序?

thanks a lot for suggestions.非常感谢您的建议。

MyLibraryTest looks as follow: MyLibraryTest如下所示:

    @RunWith(SpringRunner.class)
    @SpringBootTest
    @Transactional
    public class MyLibraryTest {
       @Autowired
       private MyService service;

       @Test
       public void testMyService_Save() {...}

    }

You need to make sure that the library module's pom.xml includes -您需要确保library模块的pom.xml包含 -

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

which is required for the module to use the @SpringBootTest annotation.这是模块使用@SpringBootTest注释所@SpringBootTest You might already be using the same in your app module but is not included in the library module.您可能已经在app模块中使用了相同的内容,但未包含在library模块中。


Well post the Edit, found the question to be possibly a duplicate of Unable to find a @SpringBootConfiguration when doing a JpaTest好吧发布编辑,发现问题可能是在执行 JpaTest 时无法找到 @SpringBootConfiguration 的重复

Also quoting from Thomas's answer from the same thread, here还引用了同一线程中托马斯的回答,这里

The thing about @DataJpaTest and a few other annotations is that they look for a @SpringBootConfiguration annotation in the current package, and if they cannot find it there, they traverse the package hierarchy until they find it.关于@DataJpaTest和其他一些注解的事情是他们在当前包中寻找@SpringBootConfiguration注解,如果他们在那里找不到它,他们就会遍历包层次结构直到找到它。

For example, if the fully qualified name for your test class was com.example.test.JpaTest and the one for your application was com.example.Application , then your test class would be able to find the @SpringBootApplication (and therein, the @SpringBootConfiguration ).例如,如果您的测试类的完全限定名称是com.example.test.JpaTest而您的应用程序的名称是com.example.Application ,那么您的测试类将能够找到@SpringBootApplication (并且在其中, @SpringBootConfiguration )。

If the application resided in a different branch of the package hierarchy, however, like com.example.application.Application , it would not find it.但是,如果应用程序驻留在包层次结构的不同分支中,例如com.example.application.Application ,它将找不到它。

which seems to be the case for you, where you are trying to test an application in a different module itself.对您来说似乎就是这种情况,您试图在不同的模块本身中测试应用程序。 Hence the error that you see.因此,您看到的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM