编程语言
首页 > 编程语言> > 使用Multi Maven项目安装程序测试Spring Boot应用程序的问题

使用Multi Maven项目安装程序测试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)

应用程序模块依赖于库.

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)

我的第一个猜测是库需要依赖于应用程序,但这会导致循环.

这个问题有什么解决方案吗?
如何正确构建我的应用程序?

非常感谢您的建议.

MyLibraryTest看起来如下:

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

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

    }

解决方法:

您需要确保库模块的pom.xml包含 –

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

这是模块使用@SpringBootTest注释所必需的.您可能已在应用程序模块中使用相同但未包含在库模块中.

好发布编辑,发现问题可能是Unable to find a @SpringBootConfiguration when doing a JpaTest的重复

同样引用托马斯的回答来自同一个帖子,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.

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).

If the application resided in a different branch of the package
hierarchy, however, like com.example.application.Application, it would
not find it.

这似乎是你的情况,你试图在不同的模块本身测试应用程序.因此你看到的错误.

标签:java,maven,dependencies,spring-boot-2,spring-boot-test
来源: https://codeday.me/bug/20190710/1428396.html