![](/img/trans.png)
[英]Spring Boot - No bean named 'entityManagerFactory' available
[英]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
我不明白,因為我在 memory DB
Nunyet,問題在於,雖然您正在配置 H2 將信息存儲在 memory 中,但從 Spring 和 Spring 的角度來看,您具有相同的數據庫系統,相同的事務,相同的事務,相同的數據庫系統,與您使用 H2 並將信息存儲在文件系統或其他不同的數據庫系統上的行為相同。
如果您不模擬您的存儲庫, entityManagerFactory
將嘗試創建實際的UserRepository
並期待這樣做所需的依賴項,例如您的實體管理器工廠。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.