gpt4 book ai didi

java - Spring Boot : Configuring Spring DataSource for Tests

转载 作者:行者123 更新时间:2023-12-03 11:18:54 25 4
gpt4 key购买 nike

我有一个 SpringBoot 应用程序。
我创建了这个测试:

@ContextConfiguration(classes={TestConfig.class})
@RunWith(SpringRunner.class)
@SpringBootTest
public class SuncionServiceITTest {
@Test
public void should_Find_2() {
// TODO
}
}
在哪里
@Configuration
@EnableJpaRepositories(basePackages = "com.plats.bruts.repository")
@PropertySource("local-configuration.properties")
@EnableTransactionManagement
@SpringBootApplication(exclude = {SecurityAutoConfiguration.class})
public class TestConfig {
}
和本地 configuration.properties :
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=sa
但是当我运行测试时。我收到此错误:

Caused by:org.springframework.beans.factory.NoSuchBeanDefinitionException: Nobean named 'entityManagerFactory' available


我也试过:
@EnableJpaRepositories(basePackages = "com.plats.bruts.repository", entityManagerFactoryRef="emf")
但后来我有错误:

Caused by:org.springframework.beans.factory.NoSuchBeanDefinitionException: Nobean named 'emf' available

最佳答案

看起来您缺少启动器依赖项。此启动依赖项具有配置 jpa 所需的所有必要依赖项。存储库。

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

关于java - Spring Boot : Configuring Spring DataSource for Tests,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64676649/

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