Junit基本用法

1.创建Junit Test Case

2.基本使用(以oracle数据库操作为例)

package com.csit.adminsystem1.tests;

import static org.junit.Assert.*;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties; import org.junit.After;
import org.junit.Before;
import org.junit.Test; import com.csit.adminsystem1.domains.TProduct;
import com.csit.adminsystem1.domains.TUser; public class JdbcTest {
Connection conn;
//每次执行测试时调用,在这里为数据库连接操作
@Before
public void init() throws ClassNotFoundException, SQLException{
System.out.println("数据库初始化中...");
Class.forName("oracle.jdbc.driver.OracleDriver");
Properties po = new Properties();
po.put("user", "yourUsername");
po.put("password", "yourPassword");
conn = DriverManager.getConnection("jdbc:oracle:thin:127.0.0.1:1521:orcl", po); }
//此处为调试内容,点击函数名,右键run as Junit Test
@Test
public void select() throws SQLException {
Statement ste = conn.createStatement();
ResultSet rs = ste.executeQuery("select * from t_product");
List<TProduct> users = new ArrayList<TProduct>();
while (rs.next()) {
TProduct user = new TProduct();
user.setProductId(rs.getInt("product_id"));
user.setProductName(rs.getString("product_name"));
user.setProductPrice(rs.getDouble("product_price"));
users.add(user);
}
System.out.println(users);
}
@Test
public void insert() throws SQLException {
Statement ste = conn.createStatement();
TProduct product = new TProduct(0, "可口可乐", 3.0);
StringBuffer sbf = new StringBuffer("insert into t_product (product_id,product_name,product_price) values(");
sbf.append(product.getProductId()).append(",");
sbf.append("'").append(product.getProductName() ).append("'").append(",");
sbf.append(product.getProductPrice()).append(")");
try{
ste.execute(sbf.toString());
}catch(Exception e) {
System.out.println("insert fail!"+e.getMessage());
throw e;
}
System.out.println("insert successful!");
}
@Test
public void delete() throws SQLException {
String sql = "delete from t_product where product_id = ?";
PreparedStatement ste = conn.prepareStatement(sql);
ste.setInt(1, 0);
try{
ste.execute();
}catch(Exception e) {
System.out.println("delete fail!"+e.getMessage());
throw e;
}
System.out.println("delete successful!"); }
//执行完毕后调用,关闭数据库
@After
public void end() throws SQLException {
conn.close();
System.out.println("over...");
}
}

分别执行查询,插入,查询,删除,查询操作后执行结果如下图:



Junit基本使用的更多相关文章

  1. 记一个mvn奇怪错误: Archive for required library: 'D:/mvn/repos/junit/junit/3.8.1/junit-3.8.1.jar' in project 'xxx' cannot be read or is not a valid ZIP file

    我的maven 项目有一个红色感叹号, 而且Problems 存在 errors : Description Resource Path Location Type Archive for requi ...

  2. 「译」JUnit 5 系列:条件测试

    原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...

  3. AndroidStudio — Error:Failed to resolve: junit:junit:4.12错误解决

    原博客:http://blog.csdn.net/u013443865/article/details/50243193 最近使用AndroidStudio出现以下问题: 解决:打开app下的buil ...

  4. 「译」JUnit 5 系列:环境搭建

    原文地址:http://blog.codefx.org/libraries/junit-5-setup/ 原文日期:15, Feb, 2016 译文首发:Linesh 的博客:环境搭建 我的 Gith ...

  5. [深入JUnit] 测试运行的入口

    阅读前提 了解JUnit 对JUnit的内部实现有兴趣 不妨看看[深入JUnit] @Before, @After, @Test的秘密] 代码版本: junit 4.12代码搜索工具: http:// ...

  6. 「译」JUnit 5 系列:扩展模型(Extension Model)

    原文地址:http://blog.codefx.org/design/architecture/junit-5-extension-model/ 原文日期:11, Apr, 2016 译文首发:Lin ...

  7. 「译」JUnit 5 系列:架构体系

    原文地址:http://blog.codefx.org/design/architecture/junit-5-architecture/ 原文日期:29, Mar, 2016 译文首发:Linesh ...

  8. 「译」JUnit 5 系列:基础入门

    原文地址:http://blog.codefx.org/libraries/junit-5-basics/ 原文日期:25, Feb, 2016 译文首发:Linesh 的博客:JUnit 5 系列: ...

  9. 新手入门JUnit单元测试

    首先将JUnit插件安装到Eclipse或myeclipse里面,编写完一个模块或者实体类的时候,直接右击,new一个JUnit项目,选择你想测试的实体类(模块),然后会自动生成一个类,这个类,我们将 ...

  10. [Android]使用自定义JUnit Rules、annotations和Resources进行单元测试(翻译)

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/5795091.html 使用自定义JUnit Rules.ann ...

随机推荐

  1. JAVA课程设计---学生基本信息管理系统(201521123039 王兴)

    1.团队课程设计博客链接 http://www.cnblogs.com/zyjjj/p/7061880.html 2.个人负责模块或任务说明 函数 功能说明 Search 查找学生信息,分为两种查找方 ...

  2. 201521123014 《Java程序设计》第9周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 异常(Exception)处理 异常(Exception)的概念:在程序运行的时候可能出错,Java中把程序出现的错 ...

  3. Linux-exec命令试验驱动(12)

    对于做驱动经常会使用exec来试验驱动,通过exec将-sh进程下的描述符指向我们的驱动,来实现调试 -sh进程常用描述符号: 0:标准输入 1:标准输出 2:错误信息 5:中断服务 exec命令使用 ...

  4. 代码的完整性:打印1到最大的n位数

    输入数字n,按顺序打印出从1到最大的n位十进制数. 比如,输入3,则打印出1,2,3,.....,一直到最大的3位数即999. 全排列打印 public class Main { public sta ...

  5. Bootstrap框架的了解和使用之栅格系统

       前    言 Bootstrap Bootstrap 包含了一个响应式的.移动设备优先的.不固定的网格系统,可以随着设备或视口大小的增加而适当地扩展到 12 列.它包含了用于简单的布局选项的预定 ...

  6. Opengl4.5 中文手册—G

    索引 A      B    C      D     E     F     G H      I     J      K     L     M     N O      P    Q      ...

  7. Qt学习之路MainWindow学习过程中的知识点

    一.Qt的GUI程序有一个常用的顶层窗口,叫做MainWindow MainWindow继承自QMainWindow.QMainWindow窗口分成几个主要的区域:   二.QAction类 QAct ...

  8. 每周刷题记录--by noble_

    学习hzwer的博客. ----------------------------------------------------------------- 2017.10.3 主要是水题与傻逼dp: ...

  9. NOIP2017SummerTraining0712

    个人感受:打了三个小时不到的第一题,然后也就没有多少时间去搞第二题了,特别是第二题还看到了期望这样的东西,这个难以理解,第三题的树分治,myx大佬说50分好拿,但是我觉得也挺难拿的. 单词检索 时间限 ...

  10. Linux下搭建tomcat和jre的环境

    1.下载linux版本的tomcat和jre tomcat下载:http://pan.baidu.com/s/1nt7D87J: jre下载:http://pan.baidu.com/s/1sj4hA ...