SQL Java JDBC AssertionError

时间:2016-04-05 05:52:57

标签: java sql jdbc junit

我正在对我的代码运行JUnit测试,并且我在我的方法retrieveByCustomerID上获得了AssertionError报告。我调试并意识到它由于某种原因没有执行while循环。我将发布方法和JUnit测试。断言是因为结果集应该包含至少1个值,但由于while循环不执行,所以它是一个空数组

我的SQL语句是:

final static String selectByCustomerID = 
"SELECT id, product_id, customer_id, purchase_date, purchase_amount FROM purchase WHERE customer_id = ?;";

public List<Purchase> retrieveForCustomerID(Connection connection, Long customerID) throws SQLException, DAOException {

        if (customerID == null) 
        {
            throw new DAOException("Trying to retrieve Purchase with NULL customer id");
        }

        List<Purchase> result = new ArrayList<>();
        PreparedStatement ps = null;
        try 
        {
            ps = connection.prepareStatement(selectByCustomerID);
            ps.setLong(1, customerID);
            ResultSet rs = ps.executeQuery();

            while(rs.next()) 
            {
                Purchase purch = new Purchase();
                purch.setId(rs.getLong("id"));
                purch.setProductID(rs.getLong("product_id"));
                purch.setCustomerID(rs.getLong("customer_id"));
                purch.setPurchaseDate(rs.getDate("purchase_date"));
                purch.setPurchaseAmount(rs.getDouble("purchase_amount"));
                result.add(purch);
            }

            System.out.println(result + "RESULTS \n");
            return result;
        }
        finally 
        {
        }
    }

public void testRetrieveForCustID() throws Exception
    {
        DataSource dataSource = DataSourceManager.getDataSource();
        Connection connection = dataSource.getConnection();
        PurchaseDAO pdao = new PurchaseDaoImpl();

        List<Purchase> purchases = pdao.retrieveForCustomerID(connection, customerID);
        assertTrue(purchases.size() >= 1);

        connection.close();
    }

0 个答案:

没有答案