Java断言错误

Java AssertionError

我正在尝试断言 2 个数组列表。 即使列表相同,我也会在控制台中收到错误消息 知道这里出了什么问题吗?任何帮助将不胜感激..

从数据库中提取数据并将其放入地图

    public Map<String, List<Products>> Req1() throws SQLException {

        BaseQuery bq = new BaseQuery("root", "georgespc");

        List<String> columns = new ArrayList<String>();
        columns.add("productCode");
        columns.add("productName");
        columns.add("productLine"); // 2
        columns.add("productScale");
        columns.add("productVendor");
        columns.add("productDescription");
        columns.add("quantityInStock");
        columns.add("buyPrice");
        columns.add("MSRP");

        ArrayList<ArrayList<Object>> tableOfProducts = bq.select(columns, "products");

        List<String> productLines = new ArrayList<String>();

        Map<String, List<Products>> lineWithProducts = new HashMap<String, List<Products>>();

        for (int i = 0; i < tableOfProducts.size(); i++) {

            if (!productLines.contains(tableOfProducts.get(i).get(2))) {
                productLines.add(tableOfProducts.get(i).get(2).toString());
                lineWithProducts.put(tableOfProducts.get(i).get(2).toString(), new ArrayList<Products>());
            }

        }

        for (int i = 0; i < tableOfProducts.size(); i++) {

            String ID = tableOfProducts.get(i).get(0).toString();
            String name = tableOfProducts.get(i).get(1).toString();
            String line = tableOfProducts.get(i).get(2).toString();
            String scale = tableOfProducts.get(i).get(3).toString();
            String vendor = tableOfProducts.get(i).get(4).toString();
            String desc = tableOfProducts.get(i).get(5).toString();
            Integer quantity = (Integer) tableOfProducts.get(i).get(6);
            BigDecimal price = (BigDecimal) tableOfProducts.get(i).get(7);
            BigDecimal msrp = (BigDecimal) tableOfProducts.get(i).get(8);

            Products p = new Products(ID, name, line, scale, vendor, desc, quantity, price, msrp);

            if (!lineWithProducts.get(line).contains(p)) {
                lineWithProducts.get(line).add(p);
            }

        }

        return lineWithProducts;

    }




测试CLASS

    @Test
    public void test() throws SQLException {
        Map<String, List<Products>> lineWithProducts = new HashMap<String, List<Products>>();
        BaseQuery bq = new BaseQuery("root", "georgespc");
        SetH_req1 requirements = new SetH_req1();

        ResultSet rs = bq.query("select productline from productLines");
        ResultSetMetaData rsmd = rs.getMetaData();

        while (rs.next()) {


            for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                String columnValue = rs.getString(i);
                lineWithProducts.put(columnValue, new ArrayList<Products>());

            }

        }

        rs = bq.query(
                "select productCode, productName, productline, productScale, productVendor, productDescription, quantityInStock, buyPrice, MSRP "
                        + "from products where productline='motorcycles'");
        rsmd = rs.getMetaData();
        System.out.println("\n\n\n\n");

        String line = null;

        while (rs.next()) {

            Products p = null;

            for (int i = 1; i <= rsmd.getColumnCount(); i++) {

                String ID = rs.getString(1);
                String name = rs.getString(2);
                line = rs.getString(3);
                String scale = rs.getString(4);
                String vendor = rs.getString(5);
                String desc = rs.getString(6);
                Integer quantity = rs.getInt(7);
                BigDecimal price = rs.getBigDecimal(8);
                BigDecimal msrp = rs.getBigDecimal(9);

                p = new Products(ID, name, line, scale, vendor, desc, quantity, price, msrp);

            }


            if (!lineWithProducts.get(line).contains(p)) {
                lineWithProducts.get(line).add(p);
            } 


        }

        assertEquals(requirements.Req1().get(line).get(1), lineWithProducts.get(line).get(1));



错误

java.lang.AssertionError: expected: com.com1028.assignment.Products<

ID=S10_2016, name=1996 Moto Guzzi 1100i, productType=Motorcycles, productScale=1:10, vendor=Highway 66 Mini Classics, description=Official Moto Guzzi logos and insignias, saddle bags located on side of motorcycle, detailed engine, working steering, working suspension, two leather seats, luggage rack, dual exhaust pipes, small saddle bag located on handle bars, two-tone paint with chrome accents, superior die-cast detail , rotating wheels , working kick stand, diecast metal with plastic parts and baked enamel finish., stock=6625, price=68.99, MRSP=118.94

but was: com.com1028.assignment.Products<

ID=S10_2016, name=1996 Moto Guzzi 1100i, productType=Motorcycles, productScale=1:10, vendor=Highway 66 Mini Classics, description=Official Moto Guzzi logos and insignias, saddle bags located on side of motorcycle, detailed engine, working steering, working suspension, two leather seats, luggage rack, dual exhaust pipes, small saddle bag located on handle bars, two-tone paint with chrome accents, superior die-cast detail , rotating wheels , working kick stand, diecast metal with plastic parts and baked enamel finish., stock=6625, price=68.99, MRSP=118.94

>

您正在比较两个 Products 对象,因此您应该确保重写 class' equals 方法。例如:

@Override
public boolean equals(final Object o) {
    if (o == null || this.getClass() != o.getClass()) {
        return false;
    }
    final Products products = (Products) o;
    return Objects.equals(this.id, products.id) &&
            Objects.equals(this.name, products.name) &&
            Objects.equals(this.line, products.line) &&
            Objects.equals(this.scale, products.scale) &&
            Objects.equals(this.vendor, products.vendor) &&
            Objects.equals(this.desc, products.desc) &&
            Objects.equals(this.quantity, products.quantity) &&
            Objects.equals(this.price, products.price) &&
            Objects.equals(this.msrp, products.msrp);
}

当然,如果你覆盖了equals,你也应该覆盖hashCode:

@Override
public int hashCode() {
    return Objects.hash(
            this.id,
            this.name, 
            this.line, 
            this.scale, 
            this.vendor, 
            this.desc, 
            this.quantity, 
            this.price, 
            this.msrp);
}