创建两个(junit)测试函数

kmynzznz  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(341)

我试着编写两个简单的第一个测试函数。
我要测试的第一个函数是create函数:

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(milestone))来验证这一点。那么,这种测试的真正效用是什么(如果这些测试是正确的?)我没有看到真正的区别(我是java新手)。

vmjh9lq9

vmjh9lq91#

在测试类中,您可以额外检查以下1。对于每个抛出的异常,您可以Assert是否在特定场景中抛出了特定的异常。例如。

@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.
}
}

如果插入和更新操作按需要在数据库中实际执行,则Assert。在dao层创建select方法,

public Milestone select(String milestoneId) throws ClassNotFoundException, SQLException{
select * from MILESTONE  where ID=milestoneId
transform resultset into Milestone object
return MilestoneObject;
}

并从测试方法中调用它来Assert例如。

@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());
        }

希望这有帮助。

相关问题