Junit单元测试

选择测试

边界测试

压力测试

功能测试

黑盒子

白盒子

灰盒子

Junit

可以自己进行单元测试

@Test进行标识Test是一个类,不要把Junit的类名于该类相同,通过就近原则,这样不会调用我们的系统Test方法

还可以通过创建一个Junit Test Case测试中含有@Before @After每次执行的的时候都会执行这两个语句,如果没有Test不能执行

package cn.jiedada.julit;

import static org.junit.Assert.*;

import javax.xml.stream.events.StartDocument;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class JuiltTest {
    @Before
    public void before(){
        System.out.print("先开始");
    }
    @Test
    public void eaat(){
        System.out.print("吃东西");
    }
    @Test
    public void wlak(){
        System.out.print("吃了东西就要要运动");
    }
    @After
    public void after(){
        System.out.println("结束了");
    }

}
View Code

Jarclasspath

Jar

就是一个字节码文件的压缩包,当我们需要一些我们自己写好的工具类的时候,我们不需要导入整个项目,只需要把该类导出来,作为一个jar包即可,这样既可以用我们需要的,又可以使用我们的功能

右键想要使用的自定义工具类选择Exprot然后输入jar选择路径

导入jar包

第一种建立一个和src平级的lib文件夹然后建立一个把jar包复制进去,然后右键

Classpath文件

我们写好1的内容的存储路径方式,等等

当出现红色感叹号的时候

所以我们把导入的jar包删除了就会出现这样的错误

资源文件解析

概念:通过资源文件解决硬编码问题(把输出或输入的相关参数(例如:路径、输出的形式或格式)直接以常量的方式书写在源代码)主要为通过propertiesload方法把流加载到文件中,先创建一个于src平级的resources文件夹,在添加一个后最为.properties的文件

3中方式实现

正常实现先使用properties然后用load方法

@org.junit.Test
    public void connection() throws Exception{
        Properties properties = new Properties();
        FileInputStream inStream=new FileInputStream("resources/login.properties");
        properties.load(inStream);
        String name = properties.getProperty("username");
        if(properties.getProperty("username").equals("admin")&&properties.getProperty("password").equals("pwd"))
        {
            System.out.println("登陆成功");
        }else{
            System.out.println("账号密码错误");
        }
        
    }
View Code

或者通过字节码文件

    @org.junit.Test
    public void junment() throws IOException{
        //创建properties
        Properties prop = new Properties();
        //获取字节码文件类名.class
        Class<Propret> cls= Propret.class;
        //通过字节码文件获取流
        InputStream stream = cls.getResourceAsStream("/login.properties");
        //加载到内存中来
        prop.load(stream);
        System.out.println(prop.getProperty("username"));
    }
View Code

或者

ThreadLocal

每个线程都可以获得自己的对象就算是单列模式也是一样的创建了一种新的获取线程的方式

public class ThreadLocalSingleton {
    /*
     * ThreadLocal 会每一个线程创建一个本地的变量副本、
     *    每一个线程可以获取到自己创建的变量  不同线程获取到不同的变量
     * */
    private ThreadLocalSingleton(){}
    private static ThreadLocal<ThreadLocalSingleton> local=new ThreadLocal<ThreadLocalSingleton>(){
        protected ThreadLocalSingleton initialValue() {
            return new ThreadLocalSingleton();
        };
    };
    public static ThreadLocalSingleton getIns(){
        return local.get();
    }
}
View Code
原文地址:https://www.cnblogs.com/xiaoruirui/p/11306798.html