【发布时间】:2018-02-05 13:29:09
【问题描述】:
我正在使用 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.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。
【问题讨论】:
标签: java spring testing spring-boot h2