JAVA 单元测试之JUint框架

1、单元测试(unit testing):

是指对软件中的最小可测试单元进行检查和验证,最小单元一般指的是一个函数或者一个类,执行单元测试,就是为了证明这段代码的行为和我们期望的一致,main()方法就是一种最粗糙的临时性单元测试。

2、Junit:

是一个开源的JAVA测试框架,用于编写和运行可重复的测试。
JUnit是由Erich Gamma和Kent Beck编写的一个回归测试框架(regressio testing framework)。JUnit测试是程序员测试,即所谓白盒测试,多数JAVA的开发环境都已经集成了JUnit作为单元测试的工具

3、在Eclipse中如何使用JUnit:

在这里插入图片描述在这里插入图片描述在这里插入图片描述## 标题## 标题
在这里插入图片描述

4、白盒测试实战案例:

//可以尝试测试一下这个UserDaoImpl类中的loginSelect方法和add方法

package com.login_system.dao.impl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Writer;

import com.login_system.dao.UserDao;
import com.login_system.entity.User;
import com.login_system.exception.DataAccessException;

public class UserDaoImpl implements UserDao {
//重写登录功能
@Override
public User loginSelect(String name, String password) {
User user=null;//读取到信息以后存储起来
// 读取文件,查询出一个用户的信息
BufferedReader br=null;
try {
br=new BufferedReader(new InputStreamReader(new FileInputStream(“D:\User1.txt”),“GBK”));
while(true) {
String str = br.readLine();
if(str==null) {
break;
}
//将用户信息以逗号截取成小的字符串存储起来,千万要注意英文的逗号和汉字的逗号的区别
String[] u=str.split(",");
//System.out.println(u[0]);
if(u[1].equals(name)&&u[2].equals(password)) {
user=new User(u[0],u[1],u[2],u[3],u[4]);
break;
}
}
} catch (Exception e) {
e.printStackTrace();
// 一定要在catch里面追加异常
throw new DataAccessException(“数据库连接失败!”,e);
}finally {
if(br!=null) {
try {
br.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
return user;
}
//重写注册功能
@Override
public void add(String number, String name, String password, String sex, String address) {

	String str =number+","+name+","+password+","+sex+","+address;
	byte[] buff = new byte[] {};
	FileOutputStream fos = null;
	File file = new File("D:\\User1.txt");
	if (!file.exists()) {
		try {
		file.createNewFile();
		} catch (IOException e) {
		e.printStackTrace();
		}
		}
		try {
		buff = str.getBytes();
		fos = new FileOutputStream(file, true);
		fos.write('\n');
		fos.write(buff);
		} catch (FileNotFoundException e1) {
		e1.printStackTrace();
		} catch (IOException e) {
		e.printStackTrace();
		} finally {
		  if (fos != null) {
		  try {
		    fos.close();
		    } catch (IOException e) {
		    e.printStackTrace();
		    }
		   }
		}
}

}
//测试代码:
package com.login_system.dao;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

import com.login_system.dao.impl.UserDaoImpl;
import com.login_system.entity.User;

import junit.framework.Assert;

public class UserDaoImplTest {
@BeforeClass
//BeforeClass注解的方法只执行一次
public static void beforeClass() {
System.out.println(“开启数据库”);
}
@AfterClass
//@AfterClass注解的方法只执行一次
public static void afterClass() {
System.out.println(“关闭数据库”);
}
@Before
//@Before注解的方法可多次执行
public void before() {
System.out.println(“之前执行”);
}
@After
//@After注解的方法可多次执行
public void after() {
System.out.println(“之后执行”);
}
//表示测试loginSelect方法
@Test
public void testloginSelect() {
//编写loginSelect方法的测试案例,验证loginSelect方法是否正确
UserDaoImpl daoImpl=new UserDaoImpl();
//如果出现异常,表示测试失败
User user=daoImpl.loginSelect(“张三”, “123456789”);
//使用断言,假设或失败
Assert.assertNull(user);
}
//表示测试add方法
@Test
//没有返回值的方法,一般通过分析方法里面的对象
public void tsestadd() {
//Assert.fail();//断言失败
UserDaoImpl daoImpl=new UserDaoImpl();
daoImpl.add(“10”, “张三”, “123456”, “男”, “内蒙古”);
}
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值