zoukankan      html  css  js  c++  java
  • java junit @Test 变量共享问题 --springboot 中的Controller静态变量可以共享

    junit多个测试方法共享变量_不负韶华,只争朝夕!-CSDN博客
    https://blog.csdn.net/IndexMan/article/details/85004828

    ----上面是整个类测试下来 才能拿到test1的修改的值。。  单独运行每一个test,而能共享变量吗? 验证test 2 3 拿到的都是0.

    比如给我改成下方的:

    要求先单独运行test1,然后在分别单独启动 test2 test3.。对应的使用情景就是:一个测试案例不断更新一个值,希望给另外的测试案例持续使用。

    package com.abc.clptest;
    
    import org.junit.Test;
    
    import java.util.concurrent.TimeUnit;
    
    public class ShareVariableTest {
        private static int id=0;
    
        @Test
        public void test1() throws InterruptedException {
            id=1;
            System.out.println("test1:"+id);
            while (true){ TimeUnit.SECONDS.sleep(2);
            id++;
                System.out.println("test1:"+id);
            }
        }
    
        @Test
        public void test2(){
            System.out.println("test2:"+id);
        }
    
        @Test
        public void test3()throws InterruptedException{
            System.out.println("test3:"+id);
            while (true){ TimeUnit.SECONDS.sleep(2);
                System.out.println("test3:"+id);
            }
        }
    }

    test1中输出值一直累加,

    test1:1
    test1:2
    test1:3
    test1:4
    test1:5

    但 test2 test3 拿到的一直都是0

    test3:0
    test3:0
    test3:0

    ==》 就算单独启动test 创建了多个实例,里面的静态变量不都是只有一份吗?

    拓展: 而springboot 中的Controller 就可以共享变量

     多个http请求可以共用其中的静态变量值

     --------------

    后面,改成了读取  Properties 配置文件了。呵呵

  • 相关阅读:
    linux脚本练习之将数据导入oracle表
    linux脚本之一个程序调用另一个程序
    使用客户端Navicat连接数据库oracle19c
    centos7安装与卸载oracle19c
    Redis-cluster集群搭建(redis版本5.0.4)
    linux下redis的哨兵模式
    使用POI导入Excel文件
    MySQL8.0搭建MGR集群(MySQL-shell、MySQL-router)
    MySQL Shell用法
    CentOS 7下使用rpm包安装MySQL8.0
  • 原文地址:https://www.cnblogs.com/rogge7/p/15120888.html
Copyright © 2011-2022 走看看