【发布时间】:2019-10-20 22:02:24
【问题描述】:
我们有一个依赖于 Spring Boot 2.0 的应用程序。我们正在将其从 JDK8 迁移到 JDK11。这也使我们能够将 Spring Boot 从 2.0 更新到 2.1。在阅读了变更日志后,我们似乎需要进行任何重大变更。
现在问题在于某些测试类同时使用@SpringBootTest 和@DataJpaTest 进行注释。根据this 和文档,我们不应该同时使用两者,而是将@DataJpaTest 更改为@AutoConfigureTestDatabase。代码如下:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {A.class, B.class}, properties = {
"x=xxx",
"y=yyy"
})
@AutoConfigureTestDatabase // Used to be @DataJpaTest
@EnableJpaRepositories("com.test")
@EntityScan("com.test")
public class Test {
@TestConfiguration
public static class TestConfig {
// Some beans returning
}
// Tests
}
现在,我们得到以下错误:
NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
所以按照this answer,我们做了这样的事情:
@EnableJpaRepositories(basePackages="com.test", entityManagerFactoryRef="entityManagerFactory")
即使在这之后,我们仍然会遇到同样的错误。这是删除@DataJpaTest 的正确方法吗?还是我们需要删除@SpringBootTest 并做其他事情?非常感谢任何形式的指导。
【问题讨论】:
-
假设您的应用程序使用自动配置,
@SpringBootTest应该是@DataJpaTest的超集。如果没有实体管理器工厂,则自动配置的 JPA 似乎未激活。从你迄今为止分享的内容中,我无法说出原因。你能用minimal, complete, and verifiable example更新你的问题吗? -
为什么你认为你必须同时使用 SpringBootTest 和 DataJpaTest?
-
@AndyWilkinson 我们没有使用自动配置。我将尝试制作示例,但可能需要一些时间。
-
看看这个stackoverflow.com/a/53932997/6643803,我遇到了类似的问题,我就是这样解决的。
-
在我们的项目中,我们使用@DataJpaTest 和@ContextConfiguration(locations = { "classpath:test-context.xml" }) 的组合。这是因为 testmodule 与所有其他模块隔离。在 test-context.xml 我们定义了
标签: java spring-boot