欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解spring cloud如何使用spring-test進(jìn)行單元測(cè)試

 更新時(shí)間:2019年11月11日 08:34:27   作者:happyhuangjinjin  
這篇文章主要介紹了spring cloud如何使用spring-test進(jìn)行單元測(cè)試,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

上篇和大家學(xué)習(xí)了spring cloud 如何整合reids,在測(cè)試時(shí)借用了web形式的restful接口進(jìn)行的。那還有沒有別的方式可以對(duì)spring boot和spring cloud編寫的代碼進(jìn)行單元測(cè)試呢?答案:肯定是有的。這篇講解一下如何使用 spring-boot-starter-test進(jìn)行單元測(cè)試

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>

說明:只要使用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;

@SpringBootApplication
public class TestApplication {

  public static void main(String[] args) {
    SpringApplication.run(TestApplication.class, args);
  }

}

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


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 # 連接超時(shí)時(shí)間(毫秒)
  database: 0 # Redis默認(rèn)情況下有16個(gè)分片,這里配置具體使用的分片,默認(rèn)是0
  lettuce:
   pool:
    max-active: 8 # 連接池最大連接數(shù)(使用負(fù)值表示沒有限制) 默認(rèn) 8
    max-wait: -1 # 連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒有限制) 默認(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)
@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() {
    // 測(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沒有啟動(dòng)時(shí),運(yùn)行TestRedis.java(右鍵選擇Junit Test)


連接不上Reids server異常

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


日志中打印相關(guān)數(shù)據(jù),說明數(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ù)庫的增刪查改??梢允褂胹pring中的各種注解,注入對(duì)象。

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論