Java 与@SpringBootTest一起使用时删除@DataJpaTest的推荐方法

Java 与@SpringBootTest一起使用时删除@DataJpaTest的推荐方法,java,spring-boot,Java,Spring Boot,我们有一个依赖SpringBoot2.0的应用程序。我们正在将它从JDK8迁移到JDK11。这也使我们能够将SpringBoot从2.0更新到2.1。在阅读了变更日志之后,我们似乎需要进行任何重大的变更 现在的问题在于一些测试类同时使用@SpringBootTest和@DataJpaTest进行注释。根据和以及文档,我们不应该同时使用这两种方法,而是将@DataJpaTest更改为@AutoConfigureTestDatabase。代码如下所示: @RunWith(SpringRunner.c

我们有一个依赖SpringBoot2.0的应用程序。我们正在将它从JDK8迁移到JDK11。这也使我们能够将SpringBoot从2.0更新到2.1。在阅读了变更日志之后,我们似乎需要进行任何重大的变更

现在的问题在于一些测试类同时使用
@SpringBootTest
@DataJpaTest
进行注释。根据和以及文档,我们不应该同时使用这两种方法,而是将
@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
根据,我们做了如下工作:

@EnableJpaRepositories(basePackages="com.test", entityManagerFactoryRef="entityManagerFactory")

即使在这之后,我们仍然以同样的错误结束。这是删除
@DataJpaTest
的正确方法吗?或者我们需要删除
@springbootest
并做其他事情吗?非常感谢任何类型的指导。

testclass用@DataJpaTest和@ContextConfiguration注释

@RunWith(SpringRunner.class)
@DataJpaTest
@ContextConfiguration(locations = { "classpath:test-context.xml" })
public abstract class AbstractTestCase {

    protected static final Logger LOG = LoggerFactory.getLogger(AbstractTestCase.class);

}
我们定义了一个test-context.xml。这是因为testmodule与所有其他模块(multi-maven模块项目)是隔离的。在test-context.xml中,我们为基本包定义了组件扫描

<context:component-scan base-package="de.example.base.package" />


假设您的应用程序正在使用自动配置,
@springbootest
应该是
@DataJpaTest
的超集。如果没有实体管理器工厂,则显示JPA自动配置未激活。从你到目前为止的分享,我不知道为什么。你能用一句话更新你的问题吗?为什么你认为你必须同时使用SpringBootTest和DataJpaTest?@AndyWilkinson我们没有使用自动配置。我将尝试制作这个示例,但这可能需要一些时间。看看这个,我有一个类似的问题,这就是我解决它的方法。在我们的项目中,我们使用@DataJpaTest和@ContextConfiguration(locations={“classpath:test context.xml”})的组合。这是因为testmodule与所有其他模块隔离。在test-context.xml中,我们定义了