gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 03:47:26 26 4
gpt4 key购买 nike

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

}

最佳答案

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

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

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


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

同时引用 Thomas 在同一主题中的回答,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 项目设置测试 Spring Boot 应用程序的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42606918/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com