我试着编写两个简单的第一个测试函数。
我要测试的第一个函数是一个创建函数:
public Milestone create(Milestone milestone) throws ClassNotFoundException, SQLException {
String id = UUID.randomUUID().toString();
milestone.setId(id);
Class.forName("org.h2.Driver");
Connection connection = DriverManager.getConnection("jdbc:h2:~/dao_db", "sa", "");
PreparedStatement prepareStatement = connection.prepareStatement("INSERT INTO MILESTONE VALUES(?, ?,?)");
prepareStatement.setString(1, id);
prepareStatement.setString(2, milestone.getName());
prepareStatement.setString(3, milestone.getDescription());
prepareStatement.executeUpdate();
connection.close();
return milestone;}
第二个是更新功能:
public Milestone update(Milestone milestone) throws ClassNotFoundException, SQLException{
Class.forName("org.h2.Driver");
Connection connection = DriverManager.getConnection("jdbc:h2:~/dao_db", "sa", "");
PreparedStatement prepareStatement = connection.prepareStatement("UPDATE MILESTONE SET NAME=?, DESCRIPTION = ? WHERE ID=?");
prepareStatement.setString(1, milestone.getName());
prepareStatement.setString(2, milestone.getDescription());
prepareStatement.setString(3, milestone.getId());
prepareStatement.executeUpdate();
connection.close();
return milestone;};
然后,我尝试编写这两个函数:
import static org.junit.Assert.*;
import java.sql.SQLException;
import org.junit.Test;
import model.Milestone;
public class MilestoneDAOImplTest {
@Test
public void createTest() throws ClassNotFoundException, SQLException {
MilestoneDAOImpl ms = new MilestoneDAOImpl();
Milestone milestone = new Milestone("test","test");
assertNotNull("milestone created", ms.create(milestone)); }
@Test
public void updateTest() throws ClassNotFoundException, SQLException {
MilestoneDAOImpl ms = new MilestoneDAOImpl();
Milestone milestone = new Milestone("test","test");
assertNotNull("milestone updated", ms.update(milestone)); }
如果这两个测试功能是否正确,你能告诉我吗?因为我发现做这样的测试有点没用。我的意思是在我的createTest函数中,唯一的验证是
assertNotNull("milestone created", ms.create(milestone));
但是我也可以通过做同样的事情来验证我的主类(同样的函数ms.create(里程碑)。那么,这些测试的实际效用是什么?(如果这些测试是正确的?)我没有'看到真正的区别(我是Java中的新手)。
答案 0 :(得分:0)
在您的测试课程中,您可以另外检查以下内容 1.对于抛出的每个异常,您可以断言是否在特定方案中抛出特定异常。 例如。
@Test
public void createTest() throws ClassNotFoundException, SQLException {
try{
MilestoneDAOImpl ms = new MilestoneDAOImpl();
Milestone milestone = new Milestone("test","test");
assertNotNull("milestone updated", ms.update(milestone));
}catch (ClassNotFoundException e1)
{
// do some assertions based on scenarios like some relevent jars are missed in classpath
}
catch (SQLException e2)
{
// do some assertions like wrong connection URL etc.
}
}
断言插入&根据需要在DB中执行更新操作。 在DAO层中创建select方法,
public Milestone select(String milestoneId) throws ClassNotFoundException, SQLException{
select * from MILESTONE where ID=milestoneId
transform resultset into Milestone object
return MilestoneObject;
}
从你的测试方法调用它来断言 例如。
@Test
public void updateTest() throws ClassNotFoundException, SQLException {
MilestoneDAOImpl ms = new MilestoneDAOImpl();
Milestone milestone = new Milestone("test","test");
assertNotNull("milestone updated", ms.update(milestone));
Milestone milestone1=ms.select(milestone.getId());
assertEquals(milestone1.getName(),milestone.getName());
assertEquals(milestone1.getdescrition(),milestone.getDescription());
}
希望这有帮助。