测试失败:0通过,1失败使用Junit测试我的方法

时间:2017-02-15 02:12:35

标签: java intellij-idea junit

enter image description here

我的代码如下:

@Test
public void testTrans() {

    AccountDao accountDao = new AccountDao();
    accountDao.trans();
    System.out.printf("test trans()");
}

当我使用Junit测试方法时,出现问题,以下是事件日志信息:

  

2017年2月15日
  上午10:08所有文件都是最新的

     

上午10:08测试失败:0通过,1失败

更新

这是我的AccountDao.java,在其中

public class AccountDao {

private Connection con;
private PreparedStatement pstmt;


public void trans() {

    String sql_zs = "UPDATE account SET money=money-100 WHERE accountName='zhangsan';";
    String sql_ls = "UPDATE account SET money=money+100 WHERE accountName='李四';";

    try {

        con = JdbcUtil.getConnection();   
        con.setAutoCommit(false);         
        pstmt = con.prepareStatement(sql_zs);
        pstmt.executeUpdate();


        pstmt = con.prepareStatement(sql_ls);
        pstmt.executeUpdate();

    } catch (Exception e) {
        e.printStackTrace();

        try {
            con.rollback();
        } catch (SQLException e1) {
            e1.printStackTrace();
        }

    }finally {
        JdbcUtil.closeAll(con, pstmt, null);
        try {
            con.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
}

它最终调用这种方法来改变钱,在导航中钱没有改变:

enter image description here

3 个答案:

答案 0 :(得分:1)

Maer我认为你缺少一个断言声明。假设您的.trans函数在对象内部更改了某个值,您可以断言这些更改正在发生。我将链接断言文档。 http://junit.sourceforge.net/javadoc/org/junit/Assert.html一旦你实现了一个断言方法断言某些东西是真的,你的测试应该通过。

使用断言的示例:

@Test
public void passingTest(){
  Assert.assertEquals(1, 1); // This will pass.
}

@Test
public void failingTest(){
  Assert.assertEquals(1,2); // This will fail.
}
祝你好运。很高兴看到你测试你的代码。

答案 1 :(得分:0)

我相信在执行测试用例时会有一些异常。你应该检查控制台是否有任何异常。我在你的测试用例中看不到任何断言......你想测试什么?

答案 2 :(得分:-1)

enter image description here 应该是你的测试方法没有放在测试包里的问题吧......

相关问题