import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* 测试从数据库中的某表中取出一条记录
*/
public class TestGetOneData {
private Connection conn = null;
private String url = "jdbc:mysql://localhost:3306/rmc_1";
private String driverClassName = "com.mysql.jdbc.Driver";
private String user = "root";
private String pass = "123456";
//setup()方法中放置标准资源初始化
@Before
public void setUp() throws Exception {
Class.forName(driverClassName).newInstance();
conn = DriverManager.getConnection(url, user, pass);
}
//tearDown()方法中放置标准资源回收
@After
public void tearDown() throws Exception {
conn = null;
}
//testGetOneData()方法中放置的是需要测试代码或要测试的另一个类中的方法
@Test
public void testGetOneData() {
try {
Statement st = conn.createStatement();
String sql = "select * from user";
ResultSet rs = st.executeQuery(sql);
String str = null;
while (rs.next()) {
str = rs.getString("id");
}
assertEquals("3", str);
} catch (Exception e) {
e.printStackTrace();
}
}
}
转载于:https://my.oschina.net/u/125259/blog/310203