2017-08-28 73 views
0

我使用的是Postgres数据库,我想用H2数据库进行测试。问题是当我在数据库中创建新对象时,看起来H2完全没有在测试中使用。用于SpringBoot测试的H2 db

测试类:

@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.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 

application-test.properties:

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(),它列出了由entityManager创建的postgresql和new中的所有公司。

回答

0

你应该有单独的属性文件devtest配置文件,你的情况,你应该重命名application.propertiesapplication-dev.properties和创建application.properties文件,只有一个属性spring.profiles.active=dev

+0

我之前已经尝试过了,它仍然是相同的。 :(并在application.properties我必须有spring.queries.users和spring.queries.roles,但它并不重要,在这种情况下,我认为 – Helosze

+0

您有'pom.xml'具有适当的依赖项的配置文件部分每个配置文件? –

+0

当我向配置文件添加依赖关系时,项目中存在无处不在的'无法解析':/ – Helosze