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

    1.本章学习总结 2. 书面作业 本次PTA作业题集异常 1.常用异常 题目5-1 Q1.1 截图你的提交结果(出现学号) Q1.2 自己以前编写的代码中经常出现什么异常.需要捕获吗(为什么)?应如何 ...

  2. 【DDD】业务建模实践 —— 删除帖子

    本文是基于上一篇‘业务建模战术’的实践,主要讲解‘删除帖子’场景的业务建模,包括:业务建模.业务模型.示例代码:示例代码会使用java编写,文末附有github地址.相比于<领域驱动设计> ...

  3. Ansible系列(二):选项和常用模块

    html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...

  4. Linux的诞生史

    Linux的诞生史 目录 Multics计划--开始 自由的产物-BSD GUN计划的产生 导火索MINIX Linux的诞生 Linux的标志物 Linux的现状 Multics计划--开始. 这是 ...

  5. 接口测试入门(4)--接口自动化测试框架 / list和map用法 / 随机选取新闻 (随机数生成) / 接口相关id映射

    一.接口自动化测试框架 为了更好的组织测试方法,测试用例并且持续集成,我们选择了  java+testNG(测试用例组织)+gitlab(代码版本管理)+Jenkins(持续集成工具) 作为一整套的自 ...

  6. StringBuffer的添加与删除功能

    StringBuffer的添加功能A* public StringBuffer append(String str): * 可以把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身 B* pu ...

  7. 用wrk测试nginx/ndoejs/golang

    sudo taskset -c ./wrk -c1 -t1 -d30 http://localhost/hello wrk+nginx(helloworld module) sudo taskset ...

  8. 常见sql技巧

    一.一些常见的SQL实践 (1)负向条件查询不能使用索引 select * from order where status!=0 and stauts!=1 not in/not exists都不是好 ...

  9. 第5章 不要让线程成为脱缰的野马(Keeping your Threads on Leash) ---干净的终止一个线程

    干净的终止一个线程  我曾经在第2章产生一个后台线程,用以输出一张屏幕外的 bitmap 图.我们必须解决的一个最复杂的问题就是,如果用户企图结束程序,而这张bitmap 图尚未完成,怎么办?第2章的 ...

  10. Reshape the Matrix

    In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...