首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >创建两个(junit)测试函数

创建两个(junit)测试函数
EN

Stack Overflow用户
提问于 2016-03-28 07:18:51
回答 1查看 78关注 0票数 0

我试着编写我的第一个测试函数,这两个函数很简单。

我要测试的第一个函数是create函数:

代码语言:javascript
复制
    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;}

第二个是更新函数:

代码语言:javascript
复制
    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;};

然后,我尝试编写这两个函数:

代码语言:javascript
复制
    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函数中,唯一的验证是

代码语言:javascript
复制
    assertNotNull("milestone created", ms.create(milestone));

但我也可以通过执行相同的操作(相同的函数ms.create(里程碑))在我的主类中验证这一点。那么,这种测试的真正用途是什么呢?(如果这些测试是正确的?)我没有看到真正的不同(我是Java新手)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-28 08:55:27

在您的测试类中,您还可以在1以下进行检查。对于每个抛出的异常,您可以断言是否在特定情况下抛出了特定异常。就像。

代码语言:javascript
复制
   @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.
}
}
  1. Assert插入和更新操作按需要在DB中实际执行。在DAO层中创建选择方法, 公共里程碑选择(字符串milestoneId)抛出ClassNotFoundException,SQLException{ select *从里程碑中将结果集转换为里程碑对象返回MilestoneObject;}

并从您的测试方法调用它来断言例如。

代码语言:javascript
复制
   @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());
        }

希望这能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36257780

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档