springcloud中怎么利用spring-test實(shí)現(xiàn)單元測(cè)試-創(chuàng)新互聯(lián)

這篇文章將為大家詳細(xì)講解有關(guān)springcloud中怎么利用spring-test實(shí)現(xiàn)單元測(cè)試,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

創(chuàng)新互聯(lián)成立與2013年,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站建設(shè)、成都網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元高唐做網(wǎng)站,已為上家服務(wù),為高唐各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18982081108

1、新建項(xiàng)目sc-test,對(duì)應(yīng)的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>

說(shuō)明:只要使用spring-boot-starter-test即可,該jar已經(jīng)包含spring-boot-test

2、新建spring boot啟動(dòng)類

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

備注:如果沒(méi)有該類,spring-test啟動(dòng)將報(bào)錯(cuò),見(jiàn)下圖

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: 9005spring: application:  name: sc-redis redis:  host: 127.0.0.1  password:   port: 6379  timeout: 10000 # 連接超時(shí)時(shí)間(毫秒)  database: 0 # Redis默認(rèn)情況下有16個(gè)分片,這里配置具體使用的分片,默認(rèn)是0  lettuce:   pool:    max-active: 8 # 連接池較大連接數(shù)(使用負(fù)值表示沒(méi)有限制) 默認(rèn) 8    max-wait: -1 # 連接池較大阻塞等待時(shí)間(使用負(fù)值表示沒(méi)有限制) 默認(rèn) -1    max-idle: 8 # 連接池中的較大空閑連接 默認(rèn) 8    min-idle: 0 # 連接池中的最小空閑連接 默認(rèn) 0

5、新建測(cè)試類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)@SpringBootTestpublic 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() {    // 測(cè)試線程安全//    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("[字符緩存結(jié)果] - [{}]", 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對(duì)象    final User user = (User) redisCacheTemplate.opsForValue().get(key);    log.info("[對(duì)象緩存結(jié)果] - userName={}, age={}, position={}", //        user.getUserName(), user.getAge(), user.getPosition());  }}

6、進(jìn)行測(cè)試

(1)reids server沒(méi)有啟動(dòng)時(shí),運(yùn)行TestRedis.java(右鍵選擇Junit Test)

連接不上Reids server異常

(2)reids server啟動(dòng)后時(shí),運(yùn)行TestRedis.java,出現(xiàn)綠條說(shuō)明執(zhí)行代碼成功

日志中打印相關(guān)數(shù)據(jù),說(shuō)明數(shù)據(jù)也存貯到redis server中

7、使用redis-cli驗(yàn)證數(shù)據(jù)是否正在存檔redis server中

有了spring-boot-starter-test,就可以不使用restful接口對(duì)spring boot寫的接口進(jìn)行單元測(cè)試了。不但可以測(cè)試redis,也可以測(cè)試數(shù)據(jù)庫(kù)的增刪查改。可以使用spring中的各種注解,注入對(duì)象。

關(guān)于springcloud中怎么利用spring-test實(shí)現(xiàn)單元測(cè)試就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

新聞名稱:springcloud中怎么利用spring-test實(shí)現(xiàn)單元測(cè)試-創(chuàng)新互聯(lián)
URL地址:http://bm7419.com/article34/hsipe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、動(dòng)態(tài)網(wǎng)站外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈、定制開(kāi)發(fā)、商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)