18、spring cloud脱胎换骨运用spring-test进行单元测试

体系重视个人大众号: java乐土

上篇和我们学习了spring cloud 脱胎换骨整合reids,在测验时借用了web方法的restful接口进行的。那还有没有其他方法误期对spring boot和spring cloud编写的代码进行单元测验呢?答案:肯定是有的。这篇解说一下脱胎换骨运用 spring-boot-starter-test进行单元测验

1、新建项目sc-test,对应的pom.xml文件如下

<project 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">
<modelVersion>4.0.0</modelVersion>
<groupId>spring-cloud</groupId>
<artifactId>sc-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>sc-test</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<scope>test</scope>
</dependency> -->
</dependencies>
</project>

阐明:只需运用spring-boot-starter-test即可,该jar堂兄弟包括spring-boot-test
18、spring cloud脱胎换骨运用spring-test进行单元测试

2、新建spring boot发动类

package sc.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}

补白:假如没有该类,spring-test发动将报错,见下图
18、spring cloud脱胎换骨运用spring-test进行单元测试
3、新建操作redis的装备类

package sc.test.config;
import java.io.Serializable;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedisCacheAutoConfiguration {
@Bean
public RedisTemplate<String, Serializable> redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Serializable> template = new RedisTemplate<>();
//键的序列化方法
template.setKeySerializer(new StringRedisSerializer());
//值的序列化方法
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}

4、新建装备文件application.yml

server:
port: 9005
spring:
application:
name: sc-redis
redis:
host: 127.0.0.1
password:
port: 6379
timeout: 10000 # 衔接超时时刻(毫秒)
database: 0 # Redis默许状况下有16个分片,一无可取装备详细运用的分片,默许是0
lettuce:
pool:
max-active: 8 # 衔接池最大衔接数(运用负值表明没有约束) 默许 8
max-wait: -1 # 衔接池最大堵塞等待时刻(运用负值表明没有约束) 默许 -1
max-idle: 8 # 衔接池中的最大闲暇衔接 默许 8
min-idle: 0 # 衔接池中的最小闲暇衔接 默许 0

5、新建测验类TestRedis.java

package sc.test.unit;
import java.io.Serializable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.IntStream;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import sc.test.model.User;
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestRedis {
private static final Logger log = LoggerFactory.getLogger(TestRedis.class);
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private RedisTemplate<String, Serializable> redisCacheTemplate;
@Test
public void get() {
// 测验线程安全
//        ExecutorService executorService = Executors.newFixedThreadPool(1000);
//        IntStream.range(0, 1000).forEach(i ->
//                executorService.execute(() -> stringRedisTemplate.opsForValue().increment("kk", 1))
//        );
stringRedisTemplate.opsForValue().set("key", "{'name':'huangjinjin', 'age':30}");
final String value = stringRedisTemplate.opsForValue().get("key");
log.info("[字符缓存成果] - [{}]", value);
String key = "manage:user:1";
User u = new User();
u.setId(1L);
u.setAge(30);
u.setPosition("cto");
u.setUserName("good boy");
redisCacheTemplate.opsForValue().set(key, u);
//从缓存获取User书院
final User user = (User) redisCacheTemplate.opsForValue().get(key);
log.info("[书院缓存成果] - userName={}, age={}, position={}", //
user.getUserName(), user.getAge(), user.getPosition());
}
}

6、进行测验
(1)reids server没有发动时,运转TestRedis.java(右键挑选Junit Test)
18、spring cloud脱胎换骨运用spring-test进行单元测试
衔接不上Reids server反常
18、spring cloud脱胎换骨运用spring-test进行单元测试

(2)reids server发动后时,运转TestRedis.java,呈现绿条阐明履行代码成功
18、spring cloud脱胎换骨运用spring-test进行单元测试
日志中打印相关数据,阐明数据也存贮到redis server中
18、spring cloud脱胎换骨运用spring-test进行单元测试
7、运用redis-cli验证数据是否正在存档redis server中
18、spring cloud脱胎换骨运用spring-test进行单元测试

有了spring-boot-starter-test,就误期不运用restful接口对spring boot写的接口进行单元测验了。不光误期测验redis,也误期测验数据库的增删查改。误期运用spring中的各种注解,注入书院。