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. NA笔记

    常用配置命令 mstsc 远程桌面控制指令(在运行中输入) cmd 运行 copy running-config start 正在启动文件 copy running-config startup-ci ...

  2. Python2.7笔记——常用技术点汇总

    目录 · 概况 · 安装 · 基础 · 基础语法 · 数据类型 · 变量 · 常量 · 字符编码 · 字符串格式化 · list · tuple · dict · set · if语句 · for语句 ...

  3. 一步步带你做vue后台管理框架(三)——登录功能

    系列教程<一步步带你做vue后台管理框架>第三课 github地址:vue-framework-wz 线上体验地址:立即体验 <一步步带你做vue后台管理框架>第一课:介绍框架 ...

  4. Windbg调试(关于句柄表的获取,32位)

    今天利用Windbg(x86)进行了获得句柄表的调试,从中获益良多,对调试步骤和按键又一次进行了熟悉,对于句柄表页的概念更是得到了进一步的清晰认识.windbg调试和句柄表不熟悉的朋友可以借鉴我的调试 ...

  5. JSP第七篇【简单标签、应用、DynamicAttribute接口】

    为什么要用到简单标签? 上一篇博客中我已经讲解了传统标签,想要开发自定义标签,大多数情况下都要重写doStartTag(),doAfterBody()和doEndTag()方法,并且还要知道SKIP_ ...

  6. Linux SSH 安装Tomcat

    tomcat的安装 1. 下载tomcat 从tomcat官网(http://tomcat.apache.org/download-70.cgi)下载tomcat的压缩包apache-tomcat-7 ...

  7. 逆向实战干货,快速定位自动捡阳光Call,或者标志

    逆向实战干货,快速定位自动捡阳光Call,或者标志 注意: 关于CE和OD的使用,这里不再多说,快速定位,默认大家已经有了CE基础,或者OD基础. 第一种方法,找Call 第一步,打开CE,搜索阳光值 ...

  8. js Date() 浏览器兼容问题解决

    一般 直接new Date() 是不会出现兼容性问题的,而 new Date(datetimeformatstring) 常常会出现浏览器兼容性问题,为什么,datetimeformatstring中 ...

  9. 树状数组初步_ZERO

    原博客:树状数组 1 一维树状数组 1 什么是树状数组        树状数组是一个查询和修改复杂度都为log(n)的数据结构,假设数组A[1..n],那么查询A[1]+-+A[n]的时,间是log级 ...

  10. MUI开发记录

    最近很久没有更新博客了,因为一直在学习前端h5 手机app的开发.曾经一度觉得自己css和js学得不错,进入到前端领域后才发现水很深~ HUuilder使用安卓模拟器 安卓模拟器有很多,我这里以夜神模 ...