gpt4 book ai didi

java - 无法注册 MBean [HikariDataSource (testdb)]

转载 作者:行者123 更新时间:2023-11-30 06:28:48 35 4
gpt4 key购买 nike

我想在新项目中使用 Spring Data JPA。我基本上遵循https://spring.io/guides/gs/accessing-data-jpa/中描述的设置.

当我启动应用程序或开始单个测试时,一切正常。

一旦我同时开始两个测试,Spring 就无法在第二个测试中加载 ApplicationContext,因为它失败了:

java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.jmx.export.UnableToRegisterMBeanException: Unable to register MBean [HikariDataSource (testdb)] with key 'dataSource'; nested exception is javax.management.InstanceAlreadyExistsException: com.zaxxer.hikari:name=dataSource,type=HikariDataSource

如何避免第二次尝试注册 HikariDataSource?

这是我的 build.gradle

dependencies {
compileOnly "org.projectlombok:lombok:1.16.12"

compile "org.springframework.boot:spring-boot-starter-web"
compile "org.springframework.boot:spring-boot-starter-data-jpa"

compile "com.h2database:h2"

compile "org.apache.commons:commons-lang3:3.6"

testCompile "org.junit.jupiter:junit-jupiter-api:5.0.0"
testCompile "org.junit.jupiter:junit-jupiter-params:5.0.0"

testRuntime "org.junit.platform:junit-platform-launcher:1.0.0"
testRuntime "org.junit.jupiter:junit-jupiter-engine:5.0.0"

testCompile "org.springframework.boot:spring-boot-starter-test"
}

和第一个测试类

@SpringJUnitConfig(Application.class)
class FirstRepositoryTest {
@Autowired
private FirstRepository firstRepository;

使用junit 4和@RunWith(SpringJUnit4ClassRunner.class)解决了问题,所以肯定是junit 5和spring交互导致的。

最佳答案

您需要为所有测试加载一次 Spring Context,例如 post它使用@ContextConfiguration:

that problem was loading the Spring Context with each test class. The solution was restructuring the test classes by extending an AbstractContext base class which loaded a Spring Context for all tests to share, and all the Beans therein.

关于java - 无法注册 MBean [HikariDataSource (testdb)],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46546647/

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