我想在一个新项目中使用。我基本上遵循了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的交互引起的。
发布于 2017-10-16 12:48:27
您需要为所有测试加载Spring上下文一次,例如,请参阅帖子,它使用@ContextConfiguration
这个问题是用每个测试类加载Spring上下文。解决方案是通过扩展AbstractContext基类来重构测试类,它加载了一个Spring上下文,以便所有测试共享,以及其中的所有Beans。
https://stackoverflow.com/questions/46546647
复制相似问题