springboot test测试类_Spring Boot中文参考指南46.3.16、自动配置的 Data MongoDB 测试

上一篇[46.3.11、自动配置的Spring WebFlux测试]

下一篇[46.3.21自动配置的Spring REST Docs测试]

557736a7ac12ca33e1c4dc895a349fc2.png
英文原文:https://docs.spring.io/spring-boot/docs/2.1.6.RELEASE/reference/html/boot-features-testing.html
GitHub:https://github.com/jijicai/Spring/tree/master/spring-boot

46.3.16、自动配置的 Data MongoDB 测试

你可以使用 @DataMongoTest 测试 MongoDB 应用程序。默认情况下,它配置内存中嵌入的 MongoDB(如果可用),配置 MongoTemplate,扫描 @Document 类,并配置 Spring Data MongoDB 存储库。常规 @Component bean 未加载到 ApplicationContext 中。(有关在 Spring Boot 中使用 MongoDB 的更多信息,请参阅本章之前的第 32.2 节:MongoDB。)(https://docs.spring.io/spring-boot/docs/2.1.6.RELEASE/reference/html/boot-features-nosql.html#boot-features-mongodb )

提示:可以在附录中找到由 @DataMongoTest 启用的自动配置的列表。(https://docs.spring.io/spring-boot/docs/2.1.6.RELEASE/reference/html/test-auto-configuration.html )

以下类显示了正在使用的 @DataMongoTest 注解:

79d98665d4f1b60d8e7c333a60a2e67a.png
import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;import org.springframework.data.mongodb.core.MongoTemplate;import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)@DataMongoTestpublic class ExampleDataMongoTests {    @Autowired    private MongoTemplate mongoTemplate;    //}

内存中嵌入的 MongoDB 通常可以很好地用于测试,因为它速度快,不需要任何开发者安装。但是,如果你希望对真实的 MongoDB 服务器运行测试,则应排除嵌入式 MongoDB 的自动配置,如下面示例所示:

13b52573b2dfc846173370e9c0389a3b.png
import org.junit.runner.RunWith; import org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration;import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)@DataMongoTest(excludeAutoConfiguration = EmbeddedMongoAutoConfiguration.class)public class ExampleDataMongoNonEmbeddedTests {}

46.3.17、自动配置的 Data Neo4j 测试

你可以使用 @DataNeo4jTest 测试 Neo4j 应用程序。默认情况下,它使用内存中嵌入的 Neo4j(如果嵌入式驱动程序可用),扫描 @NodeEntity 类,并配置 Spring Data Neo4j 存储库。常规 @Component bean 未加载到 ApplicationContext 中。(有关在 Spring Boot 中使用 Neo4J 的更多信息,请参见本章之前的第 32.3 节:Neo4j)。(https://docs.spring.io/spring-boot/docs/2.1.6.RELEASE/reference/html/boot-features-nosql.html#boot-features-neo4j )

提示:可以在附录中找到由 @DataNeo4jTest 启用的自动配置的列表。(https://docs.spring.io/spring-boot/docs/2.1.6.RELEASE/reference/html/test-auto-configuration.html )

以下示例显示了在 Spring Boot 中使用 Neo4J 测试的典型设置:

d1d7edb2fe94f2d11df4486d4035f013.png
import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.autoconfigure.data.neo4j.DataNeo4jTest;import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)@DataNeo4jTestpublic class ExampleDataNeo4jTests {    @Autowired    private YourRepository repository;    //}

默认情况下,Data Neo4j 测试是事务性的,并在每个测试结束时回滚。有关更多详细信息,请参见 Spring Framework 参考文档中的相关部分。如果这不是你想要的,你可以为一个测试或整个类禁用事务管理,如下所示:

8bb3006c10f38b43d64cb7ba4ece2865.png
import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.boot.test.autoconfigure.data.neo4j.DataNeo4jTest;import org.springframework.test.context.junit4.SpringRunner;import org.springframework.transaction.annotation.Propagation;import org.springframework.transaction.annotation.Transactional;@RunWith(SpringRunner.class)@DataNeo4jTest@Transactional(propagation = Propagation.NOT_SUPPORTED)public class ExampleNonTransactionalTests {}

46.3.18、自动配置的 Data Redis 测试

你可以使用 @DataRedisTest 测试 Redis 应用程序。默认情况下,它会扫描 @RedisHash 类并配置 Spring Data Redis 存储库。常规 @Component bean 未加载到 ApplicationContext 中。(有关在 Spring Boot 中使用 Redis 的更多信息,请参阅本章之前的第 32.1 节:Redis。)(https://docs.spring.io/spring-boot/docs/2.1.6.RELEASE/reference/html/boot-features-nosql.html#boot-features-redis)

提示:可以在附录中找到由 @DataRedisTest 启用的自动配置的列表。(https://docs.spring.io/spring-boot/docs/2.1.6.RELEASE/reference/html/test-auto-configuration.html )

以下示例展示了正在使用的 @DataRedisTest 注解:

ddf2b8d573319b9059cd61c639c50977.png
import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.autoconfigure.data.redis.DataRedisTest;import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)@DataRedisTestpublic class ExampleDataRedisTests {    @Autowired    private YourRepository repository;    //}

46.3.19、自动配置的 Data LDAP 测试

你可以使用 @DataLdapTest 测试 LDAP 应用程序。默认情况下,它配置内存中嵌入的 LDAP(如果可用),配置 LdapTemplate,扫描 @Entry 类,并配置 Spring Data LDAP 存储库。常规 @Component bean 未加载到 ApplicationContext 中。(有关在 Spring Boot 中使用 LDAP 的更多信息,请参阅本章之前的第 32.9 节:LDAP。)(https://docs.spring.io/spring-boot/docs/2.1.6.RELEASE/reference/html/boot-features-nosql.html#boot-features-ldap )

可以在附录中找到由 @DataLdapTest 启用的自动配置的列表。(https://docs.spring.io/spring-boot/docs/2.1.6.RELEASE/reference/html/test-auto-configuration.html )

以下示例展示了正在使用的 @DataLdapTest 注解:

da56f887abcbff8fb06c2c2b19bb821e.png
import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.autoconfigure.data.ldap.DataLdapTest;import org.springframework.ldap.core.LdapTemplate;import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)@DataLdapTestpublic class ExampleDataLdapTests {    @Autowired    private LdapTemplate ldapTemplate;    //}

内存中嵌入的 LDAP 通常可以很好地用于测试,因为它速度快,不需要任何开发者安装。但是,如果你希望对真实的 LDAP 服务器运行测试,则应排除嵌入式 LDAP 的自动配置,如下面示例所示:

aff0593870799501bc49c1c6ee61a841.png
import org.junit.runner.RunWith;import org.springframework.boot.autoconfigure.ldap.embedded.EmbeddedLdapAutoConfiguration;import org.springframework.boot.test.autoconfigure.data.ldap.DataLdapTest;import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)@DataLdapTest(excludeAutoConfiguration = EmbeddedLdapAutoConfiguration.class)public class ExampleDataLdapNonEmbeddedTests {}

46.3.20、自动配置的 REST 客户端

你可以使用 @RestClientTest 注解来测试 REST 客户端。默认情况下,它会自动配置 Jackson、 GSON 和 Jsonb 支持,配置 RestTemplateBuilder,并添加对 MockRestServiceServer 的支持。常规 @Component bean 未加载到 ApplicationContext 中。

提示:可以在附录中找到由 @RestClientTest 启用的自动配置的列表。(https://docs.spring.io/spring-boot/docs/2.1.6.RELEASE/reference/html/test-auto-configuration.html )

要测试的特定 bean 应使用 @RestClientTest 的 value 或 components 属性指定,如下面示例所示:

d03af6f06c4168199070417b25575dea.png
@RunWith(SpringRunner.class)@RestClientTest(RemoteVehicleDetailsService.class)public class ExampleRestClientTest {    @Autowired    private RemoteVehicleDetailsService service;    @Autowired    private MockRestServiceServer server;    @Test    public void getVehicleDetailsWhenResultIsSuccessShouldReturnDetails()            throws Exception {        this.server.expect(requestTo("/greet/details"))                .andRespond(withSuccess("hello", MediaType.TEXT_PLAIN));        String greeting = this.service.callRestService();        assertThat(greeting).isEqualTo("hello");    }}

上一篇[46.3.11、自动配置的Spring WebFlux测试]

下一篇[46.3.21自动配置的Spring REST Docs测试]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值