首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法注册MBean [HikariDataSource (testdb)]

无法注册MBean [HikariDataSource (testdb)]
EN

Stack Overflow用户
提问于 2017-10-03 14:09:22
回答 1查看 2.2K关注 0票数 1

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

当我启动应用程序或启动单个测试时,一切都很好。

一旦我一起启动了两个测试,Spring就无法在第二个测试中加载ApplicationContext,因为它失败了:

代码语言:javascript
复制
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

代码语言:javascript
复制
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"
}

第一堂考试课

代码语言:javascript
复制
@SpringJUnitConfig(Application.class)
class FirstRepositoryTest {
    @Autowired
    private FirstRepository firstRepository;

使用junit 4和@RunWith(SpringJUnit4ClassRunner.class)解决了这个问题,因此它必须是由junit 5和spring的交互引起的。

EN

回答 1

Stack Overflow用户

发布于 2017-10-16 12:48:27

您需要为所有测试加载Spring上下文一次,例如,请参阅帖子,它使用@ContextConfiguration

这个问题是用每个测试类加载Spring上下文。解决方案是通过扩展AbstractContext基类来重构测试类,它加载了一个Spring上下文,以便所有测试共享,以及其中的所有Beans。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46546647

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档