下一篇[46.3.21自动配置的Spring REST Docs测试]
英文原文: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 表明:
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 的自动配置,如下面示例所示:
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 测试的范例设置:
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 参考文档中的干系部分。如果这不是你想要的,你可以为一个测试或全体类禁用事务管理,如下所示:
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 表明:
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 表明:
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 的自动配置,如下面示例所示:
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 属性指定,如下面示例所示:
@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测试]