Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.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 用于SpringBoot测试的H2 db_Java_Spring_Testing_Spring Boot_H2 - Fatal编程技术网

Java 用于SpringBoot测试的H2 db

Java 用于SpringBoot测试的H2 db,java,spring,testing,spring-boot,h2,Java,Spring,Testing,Spring Boot,H2,我正在使用Postgres数据库,我想使用H2数据库进行测试。问题是,当我在数据库中创建新对象时,似乎在测试中根本没有使用H2 测试等级: @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest @AutoConfigureMockMvc @WithMockUser(roles = "ADMIN") @ActiveProfiles("test") public class CompanyTests { @SpyBean pri

我正在使用Postgres数据库,我想使用H2数据库进行测试。问题是,当我在数据库中创建新对象时,似乎在测试中根本没有使用H2

测试等级:

 @RunWith(SpringJUnit4ClassRunner.class)
 @SpringBootTest
 @AutoConfigureMockMvc
 @WithMockUser(roles = "ADMIN")
 @ActiveProfiles("test")
 public class CompanyTests {

@SpyBean
private CompanyService companyServiceMock;

@Autowired
private MockMvc mockMvc;

@Autowired
EntityManager entityManager;

@Test
@Transactional
public void testaaaa() {
    entityManager.persist(new Company("nnnn", new Address()));
    List<Company> all = companyServiceMock.findAll();
    all.forEach(company -> System.out.println(company.getName()));
}
application-test.properties:

 spring.datasource.driverClassName=org.postgresql.Driver
 spring.datasource.url=jdbc:postgresql://localhost:5432/EDI
 spring.datasource.username=postgres
 spring.datasource.password=password
 spring.datasource.platform=postgresql
 spring.datasource.initialize=true
 spring.datasource.continue-on-error=true
 spring.jackson.serialization.fail-on-empty-beans=false

 spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
 spring.jpa.generate-ddl=true
 spring.jpa.show-sql=true
 spring.jpa.hibernate.ddl-auto=create
spring.datasource.initialize=true
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-        
1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.h2.console.enabled=true

当我在测试中使用findAll()时,它会列出postgresql和entityManager创建的new中的所有公司。

您应该为
dev
test
配置文件创建单独的属性文件,在您的情况下,您应该将
application.properties
重命名为
application-dev.properties
,并使用一个属性创建
application.properties
文件
spring.profiles.active=dev
您应该为
dev
test
配置文件创建单独的属性文件,在您的情况下,您应该将
application.properties
重命名为
application-dev.properties
,并使用一个属性创建
application.properties
文件
spring.profiles.active=dev
添加
@TestPropertySource(locations=“classpath:application test.properties”)怎么样
是否要添加注释?您应该为测试类明确说明您正在使用test应用程序.properties


虽然已经晚了,但我很乐意提供一些帮助。

@TestPropertySource(locations=“classpath:application test.properties”)
添加到注释中如何?您应该为测试类明确说明您正在使用test应用程序.properties


现在已经很晚了,但我很高兴能帮上忙。

我以前试过,但还是一样的:(在application.properties中,我必须有spring.querys.users和spring.querys.roles,但在这种情况下,这并不重要,我想您的
pom.xml
中是否有配置文件部分,每个配置文件都有相应的依赖项?当我向配置文件添加依赖项时,项目中到处都有“无法解决的问题”。/我以前尝试过它而且还是一样的(在application.properties中,我必须有spring.querys.users和spring.querys.roles,但在这种情况下并不重要,我想您的
pom.xml
中是否有配置文件部分,每个配置文件都有相应的依赖项?当我向配置文件添加依赖项时,项目中到处都有“无法解决的问题”:/