使用JUnit4进行数据库访问时延迟?

问题描述 投票:0回答:1

我正在开发一个需要JUnit版本4的Java控制台应用程序。我想进行一个测试,检查User对象中的布尔字段是否成功从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);

    }

此测试失败。如果我将其分为两个测试,则一个通过而另一个失败,然后相反。看来我的吸气剂没有从数据库中获取新的更新值,但是在测试完成之后,该值肯定会更新。我很肯定这两种方法都可以通过访问我的DAO层在我的应用程序中使用。

我正在使用Spring,Oracle SQL Developer和一个AWS数据库。有人可以帮助我发现问题,无论是订单问题还是某种计时问题?

java testing oracle-sqldeveloper junit4
1个回答
0
投票

您正在执行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);
    }
© www.soinside.com 2019 - 2024. All rights reserved.