如果我只想测试 JdbcTemplate 代码,那么@DataJpaTest 的等价物是什么? [英] What is the equivalent of @DataJpaTest if I just want to test JdbcTemplate code?

查看:20
本文介绍了如果我只想测试 JdbcTemplate 代码,那么@DataJpaTest 的等价物是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring Boot 1.4 提供了一些出色的测试改进.一个是 @DataJpaTest 注释,它只连接 JPA 测试所需的部分.将 JdbcTemplate 测试所需的部分连接起来,等效的外观是什么样的?

我很好地构建了我自己的模拟 @DataJpaTest 复合注释.

解决方案

好问题.具有讽刺意味的是,昨天在 SpringOne 平台的测试演讲中提出了这一点.让我们看看实现这种专用的测试注释需要什么.

TL;DR 查看github上的代码

首先,您需要创建注释.这个注解重用了 spring-boot-test-autoconfigure 模块中的一些位.您可能想要自动配置内存数据库(如 DataJpaTest 所做的那样).您还希望确保默认情况下配置和禁用缓存(如果您的 Spring Boot 应用程序上有 @EnableCaching).您还希望默认情况下所有测试都是 @Transactional,因此您应该添加它.

接下来,您希望切片有效地启动.此时您只需要一个 DataSource、一个 JdbcTemplate、数据库迁移(flyway/liquibase)和一个事务manager 来处理 @Transactional.为避免启动其他自动配置,您应该添加以下内容:

@OverrideAutoConfiguration(enabled = false)

然后,您要明确启用上述自动配置.为此,您添加 @ImportAutoConfiguration 并在 META-INF/spring.factories

中添加以下内容

# AutoConfigureDataJpa 自动配置导入com.example.test.autoconfigure.jdbc.DataJdbcTest=org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration,org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration

spring.factories 中的键应该与注释的 FQN 匹配.每当 Spring Boot 找到没有额外属性的 @ImportAutoConfiguration 时,它就会在 spring.factories 中寻找匹配注解类型的键.

接下来,您希望能够通过过滤器包含其他组件(组件扫描).为此,您可以添加 @TypeExcludeFilters(DataJdbcTypeExcludeFilter.class) 其中 DataJdbcTypeExcludeFilterDataJpaTypeExcludeFilter 几乎相同(所以我们可能想为此提取一个公共类).

一旦你这样做了,你只需要添加你的注释,你的 JdbcTemplate 就会为你自动配置

@RunWith(SpringRunner.class)@DataJdbcTest公共类 DataJdbcSampleTests {@自动连线私人 JdbcTemplate jdbcTemplate;...}

Spring Boot 1.4 offers some fantastic testing improvements. One is the @DataJpaTest annotation where it wires up just the parts needed for JPA testing. What would the equivalent look like for just wiring up the parts needed for JdbcTemplate tests?

I'm fine constructing my own composite annotation that mimics the @DataJpaTest one.

解决方案

Good question. Ironically enough, that one was raised during the testing talk yesterday at SpringOne Platform. Let's see what it takes to implement such dedicated test annotation.

TL;DR check the code on github

First of all you need to create the annotation. This annotation reuses some bits from the spring-boot-test-autoconfigure module. You may want to auto-configure an in-memory database (like DataJpaTest does). You also want to make sure that caching is configured and disabled by default (in case you have @EnableCaching on your Spring Boot application). You also want that all your tests are @Transactional by default so you should add that.

Next, you want that slicing effectively kicks in. All you need at this point is a DataSource, a JdbcTemplate, database migrations (flyway/liquibase) and a transaction manager to process @Transactional. To avoid the other auto-configurations to kick in you should add the following:

@OverrideAutoConfiguration(enabled = false)

Then, you want to explicitly enable the auto-configurations above. In order to do so, you add @ImportAutoConfiguration and you add the following content in META-INF/spring.factories

# AutoConfigureDataJpa auto-configuration imports
com.example.test.autoconfigure.jdbc.DataJdbcTest=
org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,      
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,    
org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration,
org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration

The key in spring.factories should match the FQN of your annotation. Whenever Spring Boot finds @ImportAutoConfiguration with no extra attributes, it will look for a key matching the annotation type in spring.factories.

Next up you want to be able to include additional components (component scan) with a filter. In order to do that, you can add @TypeExcludeFilters(DataJdbcTypeExcludeFilter.class) where DataJdbcTypeExcludeFilter is pretty much the same thing as DataJpaTypeExcludeFilter (so we might want to extract a common class for that).

Once you've done that, you only need to add your annotation and your JdbcTemplate is auto-configured for you

@RunWith(SpringRunner.class)
@DataJdbcTest
public class DataJdbcSampleTests {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    ...
}

这篇关于如果我只想测试 JdbcTemplate 代码,那么@DataJpaTest 的等价物是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆