package com.test.db;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.junit.BeforeClass;
import org.junit.Test;
import com.test.bean.Person;
public class PersonDBTest
{
private static PersonDB personDB;
@BeforeClass
public static void init()
{
personDB = new PersonDB();
}
@Test
public void testInsert()
{
Person person = new Person();
person.setUsername("lisi");
person.setPassword("654321");
person.setAge(20);
personDB.insert(person);
Person person2 = this.getPersonByMaxId();
this.comparePersons(person, person2);
personDB.removeById(person2.getId());
}
@Test
public void testUpdate()
{
Person person = new Person();
person.setUsername("zhangsan");
person.setPassword("langsin");
person.setAge(40);
// 1. insert
personDB.insert(person);
// 2. get
Person person2 = this.getPersonByMaxId();
this.comparePersons(person, person2);
person2.setUsername("wangwu");
person2.setPassword("abcdefg");
person2.setAge(90);
// 3. update. what we want to test
personDB.update(person2);
// 4. get
Person person3 = this.getPersonByMaxId();
this.comparePersons(person2, person3);
personDB.removeById(person3.getId());
}
@Test
public void testGetById()
{
Person person = new Person();
person.setUsername("zhangsan");
person.setPassword("123456");
person.setAge(40);
personDB.insert(person);
int maxId = this.getMaxId();
Person person2 = personDB.getById(maxId);
this.comparePersons(person, person2);
personDB.removeById(maxId);
}
@Test
public void testRemoveById()
{
Person person = new Person();
person.setUsername("zhangsan");
person.setPassword("123456");
person.setAge(40);
personDB.insert(person);
int maxId = this.getMaxId();
personDB.removeById(maxId);
Person person2 = personDB.getById(maxId);
assertNull(person2);
}
private int getMaxId()
{
Connection conn = null;
int maxId = 0;
try
{
conn = Conn.getConnection();
String sql = "select max(id) as maxId from person";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
if (rs.next())
{
maxId = rs.getInt("maxId");
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
try
{
if (null != conn)
{
conn.close();
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
return maxId;
}
private Person getPersonByMaxId()
{
Connection conn = null;
Person person = null;
try
{
conn = Conn.getConnection();
String sql = "select max(id) as maxId from person";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
int maxId = 0;
if (rs.next())
{
maxId = rs.getInt("maxId");
}
String sql2 = "select * from person where id = " + maxId;
ps = conn.prepareStatement(sql2);
rs = ps.executeQuery();
if (rs.next())
{
person = new Person();
person.setId(maxId);
person.setUsername(rs.getString("username"));
person.setPassword(rs.getString("password"));
person.setAge(rs.getInt("age"));
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
try
{
if (null != conn)
{
conn.close();
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
return person;
}
// helper method
private void comparePersons(Person person1, Person person2)
{
assertEquals(person1.getUsername(), person2.getUsername());
assertEquals(person1.getPassword(), person2.getPassword());
assertEquals(person1.getAge(), person2.getAge());
}
}
分享到:
相关推荐
包涵了junit4新特性assertThat断言和mysql,oracle俩种数据库的增删改查测试
junit测试JDBC错误源代码,使用该源代码运行理论上插入数据库会插入两条数据,不知道是哪里出问题
包括探索JUnit的核心、软件测试原则、测试覆盖率与开发、使用stub进行粗粒度测试、使用mock objects进行测试、容器内测试、从Ant中运行JUnit测试、从Maven2中运行JUnit测试、持续集成工具、表示层的测试、Ajax测试、...
JUnit银行数据使用Junit 5进行数据库测试的简单示例不要忘记将Junit 5 jar添加到类路径中。 不要忘记在类路径中添加所选库的jdbc jar。 测试中使用的银行是Oracle,只需改用您的另一家银行即可。
包括探索JUnit的核心、软件测试原则、测试覆盖率与开发、使用stub进行粗粒度测试、使用mock objects进行测试、容器内测试、从Ant中运行JUnit测试、从Maven2中运行JUnit测试、持续集成工具、表示层的测试、Ajax测试、...
包括探索JUnit的核心、软件测试原则、测试覆盖率与开发、使用stub进行粗粒度测试、使用mock objects进行测试、容器内测试、从Ant中运行JUnit测试、从Maven2中运行JUnit测试、持续集成工具、表示层的测试、Ajax测试、...
包括探索JUnit的核心、软件测试原则、测试覆盖率与开发、使用stub进行粗粒度测试、使用mock objects进行测试、容器内测试、从Ant中运行JUnit测试、从Maven2中运行JUnit测试、持续集成工具、表示层的测试、Ajax测试、...
这是一个使用ormlite数据库框架进行数据库存储以及对数据库增删改查的操作,然后用单元测试框架junit对Dao类进行了测试的一个moudle。
要测试与他们正在使用maintened无论你的数据库变阵或。 状态 该库通过库分发,因此应该可以广泛使用。 版 Java版本 JUnit版本 H2版本 HSQLDB版本 科 状态 2.X + 8.0 4.12 / 5.X 1.4.200 2.5.0 active 1.1.X ...
包括探索JUnit的核心、软件测试原则、测试覆盖率与开发、使用stub进行粗粒度测试、使用mockobjects进行测试、容器内测试、从Ant中运行JUnit测试、从Maven2中运行JUnit测试、持续集成工具、表示层的测试、Ajax测试、...
包括探索JUnit的核心、软件测试原则、测试覆盖率与开发、使用stub进行粗粒度测试、使用mockobjects进行测试、容器内测试、从Ant中运行JUnit测试、从Maven2中运行JUnit测试、持续集成工具、表示层的测试、Ajax测试、...
DBUnit是JUnit的一个扩展,对于数据库驱动的项目而言(基本上所有的Web项目都是数据库驱动的),对于服务层的单元测试非常麻烦,因为不能保证每次测试时数据库都是同一个状态,所以开发者不敢写断言(assertEquals()...
ssm配置多个数据源、自己写的一个demo,按照所写的代码去修改相应的代码即可使用,配置文件只需该数据库连接,这里使用了两个数据库连接,若使用多个链接时可仿照此方法在继续添加即可,另附junit4测试类供大家测试...
(C#语言版)单元测试实例,主要功能包括:(1)输入数据到textbox,以逗号...(2)连接数据库,写出单元测试代码来测试求数组最大值的函数(数组数据来自于数据库);(3)向数据库中插入数据,针对于插入函数写出单元测试代码。
testcontainers是一个支持junit测试的java 8库,它提供了公共数据库、selenium web浏览器或任何可以在docker容器中运行的轻量级、一次性实例。
Testcontainers是一个支持JUnit测试的Java 8库,提供常见数据库,Selenium Web浏览器或可以在Docker容器中运行的任何其他东西的轻量级,一次性实例。
本软件力推如下的JUnit测试黄金法则:在测试空库上无限次运行不出错。 bug报告jerry_shen_sjf@qq.com 用如下管理员登录 用户名:160208 密码:jerry 火鸟 2015.1.5 这是Release Notes 1)v0.98版本是测试...
TestBasic是Java 8库,它支持JUnit测试,提供轻量级、一次性的公共数据库实例、SeleWeb浏览器或任何可以在坞箱中运行的其他工具。
该工具从类路径中提取所有JUnit测试用例并全部运行它们。 完成测试用例执行后,它将结果填充到数据库表中。 报告工具从数据库中获取结果数据并生成HTML报告。 该工具的亮点-开发人员无需编写代码即可集成现有的JUnit...
使用Junit编写测试代码的一般步骤是: 1.引入JUnit框架包,import junit.framework.*; 2.定义测试类名称,一般是将要测试的类名后附加Test; 3.测试类继承Junit的TestCase类; 4.实现类的构造方法,可以再构造方法...