18、springcloud如何使⽤spring-test进⾏单元测试上篇和⼤家学习了spring cloud 如何整合reids,在测试时借⽤了web形式的restful接⼝进⾏的。那还有没有别的⽅式可以对spring boot和spring cloud编写的代码进⾏单元测试呢?答案:肯定是有的。这篇讲解⼀下如何使⽤ spring-boot-starter-test进⾏单元测试
1、新建项⽬sc-test,对应的l⽂件如下
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /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></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>
<mavenpiler.source>1.8</mavenpiler.source>
<mavenpiler.target>1.8</mavenpiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apachemons</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
2、新建spring boot启动类
st;
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启动将报错,见下图
3、新建操作redis的配置类
st.config;
import java.io.Serializable;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.dis.RedisAutoConfiguration;
import t.annotation.Bean;
import t.annotation.Configuration;
import org.tion.lettuce.LettuceConnectionFactory;
import org.RedisTemplate;
import org.dis.serializer.GenericJackson2JsonRedisSerializer;
import org.dis.serializer.StringRedisSerializer;
@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
springcloud和springbootpublic class RedisCacheAutoConfiguration {
@Bean
public RedisTemplate<String, Serializable> redisCacheTemplate(LettuceConnectionFactory redisConn
ectionFactory) { RedisTemplate<String, Serializable> template = new RedisTemplate<>();
//键的序列化⽅式
template.setKeySerializer(new StringRedisSerializer());
//值的序列化⽅式
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
4、新建配置⽂件l
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
st.unit;
import java.io.Serializable;
import urrent.ExecutorService;
import urrent.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.st.context.SpringBootTest;
import org.RedisTemplate;
import org.StringRedisTemplate;
import st.context.junit4.SpringRunner;
st.model.User;
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestRedis {
private static final Logger log = Logger(TestRedis.class);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论