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. 201521123064 《Java程序设计》第9周学习总结

    1. 本章学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2. 书面作业 本次作业题集异常 Q1:常用异常 题目5-1 1.1 截图你的提交结果(出现学号) 1.2 自己以前 ...

  2. 201521123040《Java程序设计》第13周学习总结

    1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 2. 书面作业 1. 网络基础 1.1 比较ping www.baidu.com与ping cec.jmu ...

  3. 201521123106 《Java程序设计》第14周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多数据库相关内容. 2. 书面作业 1. MySQL数据库基本操作 建立数据库,将自己的姓名.学号作为一条记录插入.(截图,需出现自 ...

  4. [js高手之路]Node.js+jade+mongoose实战todolist(分页,ajax编辑,删除)

    该系列文章索引: [js高手之路]node js系列课程-创建简易web服务器与文件读写 [js高手之路]node js系列课程-图解express+supervisor+ejs用法 [js高手之路] ...

  5. 利用原生js制做数据管理平台,适合初学者学习

    摘要:数据管理平台在当今社会中运用十分广泛,我们在应用过程中,要对数据进行存储,管理,以及删除查询等操作,而我们在实际设计的时候,大牛们大多用到的是JQuery,而小白对jq理解也较困难,为了让大家回 ...

  6. AngularJS小结

    1.简介 AngularJS 通过 ng-directives 扩展了 HTML. 2.AngularJS指令 ng-app 指令定义一个AngularJS 应用程序的根元素.指令在网页加载完毕时会自 ...

  7. E - 钱币兑换问题

          在一个国家仅有1分,2分,3分硬币,将钱N兑换成硬币有很多种兑法.请你编程序计算出共有多少种兑法. Input每行只有一个正整数N,N小于32768. Output对应每个输入,输出兑换方 ...

  8. 封装好的图片滑动框架(AndroidImageSlider)

    前言 广告轮播条的重要性不言而喻.在很多类型app中出场率都很高. 今天给大家介绍一个轮播图开源项目,这个项目把轮播图需要的ViewPager跟计时器做了封装,使用极其方便,支持gradle在线依赖. ...

  9. 接口测试——httpclient介绍与请求方式详解

    httpClient工具介绍 HTTP协议可能是现在lntemet上使用得最多.最重要的协议了,越来越多的Java应用程序需要直接通过HTTP协议来访问网络资源.虽然在JDK的java.net包中已经 ...

  10. HiWord()

    #define HIWORD(I) ( ( WORD ) ( ( ( DWORD )( I ) >> 16) & 0xFFFF ) ). 这个宏传回一个WORD值(16位的无符号整 ...