Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在内存数据库中加速以测试JPA实体?_Java_Spring_Spring Boot_Jpa_H2 - Fatal编程技术网

Java 如何在内存数据库中加速以测试JPA实体?

Java 如何在内存数据库中加速以测试JPA实体?,java,spring,spring-boot,jpa,h2,Java,Spring,Spring Boot,Jpa,H2,因为我没有太多的JPA经验,所以我想编写一个自动化测试,以确保我的JPA实体正在检索我希望它们检索的记录。我的计划是为我的测试在memroy H2 DB中启动一个,并对它执行几个简单的CRUD操作,确保数据如我所期望的那样返回 我不知道如何让Spring Boot创建内存中的数据库。这是我到目前为止所做的,但不起作用 这是创建我的JPA存储库的配置。这是正确的应用程序代码,目前正在使用我的实际Oracle DB @Configuration @EnableJpaRepositories("com

因为我没有太多的JPA经验,所以我想编写一个自动化测试,以确保我的JPA实体正在检索我希望它们检索的记录。我的计划是为我的测试在memroy H2 DB中启动一个,并对它执行几个简单的CRUD操作,确保数据如我所期望的那样返回

我不知道如何让Spring Boot创建内存中的数据库。这是我到目前为止所做的,但不起作用

这是创建我的JPA存储库的配置。这是正确的应用程序代码,目前正在使用我的实际Oracle DB

@Configuration
@EnableJpaRepositories("com.name.project.webservice.dao")
@EntityScan("com.name.project.webservice.types")
public class JpaRepositoriesConfig {
    // Intentionally empty.
}
我将这个配置导入到我的测试中

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {JpaRepositoriesConfig.class})
public class JpaEntityTest {

    @Test
    public void test(){}
}
然后,我编写一个特定于测试的属性文件,
src/test/resources/application.properties
,用测试的H2配置替换应用程序的Oracle配置

spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
最后,我将H2罐添加到pom中。这个jar的存在,以及我测试中的注释,应该指示SpringBoot根据我的属性文件启动H2数据库

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>test</scope>
</dependency>
这个错误使我吃惊。我以为Spring Boot应该给我一个完整的自动管理工厂;我的应用程序从不实例化EntityManagerFactorybean,而且运行良好

因此,如果您不介意告诉我,我是否至少在正确配置的轨道上?我错过了导致此错误的哪一步?我需要手动声明entityManagerFactory吗

谢谢你的帮助

编辑:这是我的应用程序属性文件的相关部分,与上面记录的测试属性文件不同

spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url= <<database-url>>
spring.datasource.tomcat.max-active=5
spring.datasource.tomcat.initial-size=1
spring.datasource.tomcat.max-wait=20000 
spring.datasource.tomcat.max-idle=1
spring.datasource.driver类名=oracle.jdbc.driver.OracleDriver
spring.datasource.url=
spring.datasource.tomcat.max active=5
spring.datasource.tomcat.initial size=1
spring.datasource.tomcat.max wait=20000
spring.datasource.tomcat.max idle=1

我确实看到您正在为测试维护单独的属性文件,这意味着您正在使用测试属性创建datasource和entitymanager,并加载spring boot原始应用程序上下文。因此,您不需要任何额外的测试配置

@RunWith(SpringRunner.class)
@SpringBootTest
public class JpaEntityTest {

     @Test
     public void test(){}
}
您还可以使用配置文件来执行此操作,将
应用程序.properties
命名为
应用程序测试.properties
,并使用
@Profile
@ActiveProfile

@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfile("test")
@Profile("test")
public class JpaEntityTest {

     @Test
     public void test(){}
}

通过在测试类上使用以下注释,我能够将内存中的数据库添加到Spring上下文中:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { JpaRepositoriesConfig.class })
@DataJpaTest
当然,在我的pom中包括必要的依赖性:

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>test</scope>
</dependency>

com.h2数据库
氢
测试

请让我知道我所做的是否违反了任何最佳实践。

为什么需要此类
JpaRepositoriesConfig
以及为什么将其保留为空?你能展示一下原始配置是如何运行的吗?还有您的
应用程序.yml
或属性文件这是原始配置。它是空的,因为Spring Boot完成了所有的工作。另外,我已经附加了application.properties文件的重要部分。请尝试我的答案,我建议使用profiles如果我删除“classes”参数,它会给我以下错误:
java.lang.IllegalStateException:找不到@SpringBootConfiguration,您需要使用@ContextConfiguration或@springbootest(classes=…)在你的测试中
看起来我需要那个参数,对吗?另外,关于个人资料,你是对的。一旦我开始工作,我会添加它们。您的src类和测试类在同一个包结构中吗@AugustZellmersee this@AugustZellmeri推荐这种方法@Augustzellmerth测试类与它们正在测试的类位于同一个包中。(当然是在src/test/java中而不是在src/main/java中。)
@SpringBootApplication
类位于单独的Maven模块中。
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>test</scope>
</dependency>