上篇和大家学习了春云如何整合里德,在测试时借用了网络形式的restful接口进行的。那还有没有别的方式可以对弹簧引导和弹簧云编写的代码进行单元测试呢?答案:肯定是有的。这篇讲解一下如何使用spring-boot-starter-test进行单元测试
& lt;项目xmlns=" http://maven.apache.org/POM/4.0.0 " xmlns: xsi=" http://www.w3.org/2001/XMLSchema-instance " xsi: schemaLocation=" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”比; & lt; modelVersion> 4.0.0 & lt; groupId> spring-cloud & lt; artifactId> sc-test & lt; version> 0.0.1-SNAPSHOT & lt; packaging> jar & lt; name> sc-test & lt; url> http://maven.apache.org</url> & lt; parent> & lt; groupId> org.springframework.boot & lt; artifactId> spring-boot-starter-parent & lt; version> 2.0.4.RELEASE & lt;/parent> & lt; dependencyManagement> & lt; dependencies> & lt; dependency> & lt; groupId> org.springframework.cloud & lt; artifactId> spring-cloud-dependencies & lt; version> Finchley.RELEASE & lt; type> pom & lt; scope> import & lt;/dependency> & lt;/dependencies> & lt;/dependencyManagement> & lt; properties> & lt; project.build.sourceEncoding> UTF-8 & lt; maven.compiler.source> 1.8 & lt;/maven.compiler.source> & lt; maven.compiler.target> 1.8 & lt;/maven.compiler.target> & lt;/properties> & lt; dependencies> & lt; dependency> & lt; groupId> org.springframework.boot & lt; artifactId> spring-boot-starter-data-redis & lt;/dependency> & lt; dependency> & lt; groupId> org.apache.commons & lt; artifactId> commons-pool2 & lt;/dependency> & lt; dependency> & lt; groupId> org.springframework.boot & lt; artifactId> spring-boot-starter-web & lt;/dependency> & lt; dependency> & lt; groupId> org.springframework.boot & lt; artifactId> spring-boot-starter-test & lt; scope> test & lt;/dependency> & lt; !- & lt; dependency> & lt; groupId> org.springframework.boot & lt; artifactId> spring-boot-test & lt; scope> test & lt;/dependency>——比; & lt;/dependencies> & lt;/project> >之前说明:只要使用spring-boot-starter-test即可,该罐已经包含spring-boot-test
包sc.test; 进口org.springframework.boot.SpringApplication; 进口org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication 公开课TestApplication { 公共静态void main (String [] args) { SpringApplication.run (TestApplication.class, args); } } >之前备注:如果没有该类,spring测试启动将报错,见下图
包sc.test.config; 进口java.io.Serializable; 进口org.springframework.boot.autoconfigure.AutoConfigureAfter; 进口org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; 进口org.springframework.context.annotation.Bean; 进口org.springframework.context.annotation.Configuration; 进口org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; 进口org.springframework.data.redis.core.RedisTemplate; 进口org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; 进口org.springframework.data.redis.serializer.StringRedisSerializer; @ configuration @AutoConfigureAfter (RedisAutoConfiguration.class) 公开课RedisCacheAutoConfiguration { @ bean 公共RedisTemplate<字符串,Serializable>redisCacheTemplate (LettuceConnectionFactory redisConnectionFactory) { Serializable> RedisTemplate
详解春云如何使用spring测试进行单元测试