【问题标题】:Cucumber 4 with Spring Boot and JPA带有 Spring Boot 和 JPA 的 Cucumber 4
【发布时间】:2018-11-15 14:04:45
【问题描述】:

我正在使用 cucumber + cucumber-spring 4.1 来测试 SpringBoot 2.1 应用程序。这有需要在每个场景之间回滚数据库的场景,但对于我的生活来说,它无法正常工作。

我试过了:

带有多个注释的基类

`
@Transactional
@RunWith(SpringRunner.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
public abstract class SpringBootBaseIntegrationTest { .. } `

Stepdef 类如:

`
@ContextConfiguration(classes = {Application.class})
@Ignore
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
@Transactional
public class DatabaseSteps extends SpringBootBaseIntegrationTest implements En { ... }
`

使用@txn 注释功能和场景并添加'cucumber.api.spring' 胶水。

我的应用程序有:

`
@EnableJms
@EnableJpaRepositories
@EnableTransactionManagement
@SpringBootApplication
@PropertySource(ignoreResourceNotFound = false, value = "classpath:application.properties")
class Application implements CommandLineRunner { ... }
`

我使用标准的 JPA 存储库。

我的 application.properties 是:

`
spring.jpa.properties.hibernate.jdbc.time_zone=UTC
spring.datasource.initialization-mode=always
spring.jpa.hibernate.ddl-auto=create

spring.jpa.properties.hibernate.generate_statistics=false
spring.jpa.properties.hibernate.show_sql=false
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
#spring.h2.console.enabled=true
`

但无论我做什么,在任何组合中,数据仍然保留在测试之间。

任何人都可以阐明或管理工作设置吗?

干杯

【问题讨论】:

标签: spring spring-boot spring-data-jpa cucumber cucumber-jvm


【解决方案1】:

最后我想通了。我的 Cucumber 测试通过多种不同的方法(JMS、Rest)进入应用程序,因此测试不拥有通过这些服务注入的数据的事务范围。

因此,我在每个测试的前场景挂钩中截断数据库表,这似乎工作正常

【讨论】:

    猜你喜欢
    • 2021-11-09
    • 2018-05-12
    • 1970-01-01
    • 1970-01-01
    • 2020-02-25
    • 2017-02-22
    • 1970-01-01
    • 2017-12-15
    • 2017-03-23
    相关资源
    最近更新 更多