Spring Boot v2.1.0.RELEASE - 没有可用的名为 'entityManagerFactory' 的 bean
Spring Boot v2.1.0.RELEASE - No bean named 'entityManagerFactory' available
我有一个 Spring Boot v2.1.0.RELEASE
应用程序。
使用此测试配置 class:
@Configuration
@EnableJpaRepositories(basePackages = "com.bonansa")
@ComponentScan({"com.bonansa"})
@EnableTransactionManagement
@PropertySource("classpath:application.properties")
@EnableMBeanExport(registration= RegistrationPolicy.IGNORE_EXISTING)
public class ApplicationTestConfig {
}
这个application.properties文件:
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
和这个测试
@RunWith(SpringRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = ApplicationTestConfig.class)
@MockBeans({
@MockBean(UserService.class),
// @MockBean(UserRepository.class)
})
public class AuthorisationControllerTest implements WebMvcConfigurer {
private MockMvc mockMvc;
@Autowired
private WebApplicationContext wac;
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@Test
public void testA() throws Exception {
}
}
如果我不模拟 UserRepository.class
,我得到一个 Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
但我不明白,因为我使用的是内存中的 DB
Nunyet,问题是虽然你配置H2把信息存储在内存中,但是从Spring和SpringBoot的角度来看,你有相同的数据库系统,相同的事务,相同的实体,...总而言之,与您使用 H2 并将信息存储在文件系统或其他不同数据库系统上的行为相同。
如果您不模拟您的存储库,Spring 将尝试创建实际的 UserRepository
并期待这样做所需的依赖项,例如您的情况下的 entityManagerFactory
。
我有一个 Spring Boot v2.1.0.RELEASE
应用程序。
使用此测试配置 class:
@Configuration
@EnableJpaRepositories(basePackages = "com.bonansa")
@ComponentScan({"com.bonansa"})
@EnableTransactionManagement
@PropertySource("classpath:application.properties")
@EnableMBeanExport(registration= RegistrationPolicy.IGNORE_EXISTING)
public class ApplicationTestConfig {
}
这个application.properties文件:
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
和这个测试
@RunWith(SpringRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = ApplicationTestConfig.class)
@MockBeans({
@MockBean(UserService.class),
// @MockBean(UserRepository.class)
})
public class AuthorisationControllerTest implements WebMvcConfigurer {
private MockMvc mockMvc;
@Autowired
private WebApplicationContext wac;
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@Test
public void testA() throws Exception {
}
}
如果我不模拟 UserRepository.class
,我得到一个 Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
但我不明白,因为我使用的是内存中的 DB
Nunyet,问题是虽然你配置H2把信息存储在内存中,但是从Spring和SpringBoot的角度来看,你有相同的数据库系统,相同的事务,相同的实体,...总而言之,与您使用 H2 并将信息存储在文件系统或其他不同数据库系统上的行为相同。
如果您不模拟您的存储库,Spring 将尝试创建实际的 UserRepository
并期待这样做所需的依赖项,例如您的情况下的 entityManagerFactory
。