有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JUnit4数据库访问中的java延迟?

我正在开发一个需要JUnit版本4的Java控制台应用程序。我想做一个测试,检查用户对象中的布尔字段是否成功地从0更新为1,然后再次更新

    public void approveTest() throws BankException {
        // get the test user to work on
        User user = udi.accessUserObject("UNITtestApprove");

        // find their user id (it is 590)
        String user_id = user.getUser_id();

        //approve the account, test that the boolean/number set to 1
        udi.approve(user_id);
        assert(user.getApproved() == 1);

        //remove approval, test that the boolean/number reset to 0
        udi.removeApproval(user_id);
        assert(user.getApproved() == 0);

    }

这个测试失败了。如果我把它分成两个测试,一个通过,另一个失败,然后反过来。我的getter似乎没有从我的数据库中获取新的、更新的值,但在测试完成后,该值肯定会更新。当我通过访问DAO层在应用程序中使用这两种方法时,我确信这两种方法都有效

我正在使用Spring、Oracle SQL开发人员和AWS数据库。有谁能帮我发现问题,无论是订单问题还是时间问题


共 (1) 个答案

  1. # 1 楼答案

    您正在执行udi.approve(user_id),但在检查其值之前,您没有从数据库中获取最新版本。而是对更新之前获得的User对象进行断言。我觉得你需要更像:

      public void approveTest() throws BankException {
            // get the test user to work on
            final String userName = "UNITtestApprove";
            final User user = getUserByName(userName);
    
            // find their user id (it is 590)
            String userId = user.getUser_id();
    
            // approve the account, test that the boolean/number set to 1
            udi.approve(userId);
            assertEquals(1, getUserByName(userName).getApproved());
    
            // remove approval, test that the boolean/number reset to 0
            udi.removeApproval(userId);
            assertEquals(0, getUserByName(userName).getApproved());
    
        }
    
        public User getUserByName(final String userName) {
            return udi.accessUserObject(userName);
        }