Junit预期:但是是:错误 - 了解Junit

发布于 2025-02-06 20:24:52 字数 2466 浏览 1 评论 0原文

我是Junit和Testing的新手,并以此错误为由我认真对待: 预期:< null>但是:预期:< null>但是:<订单ID:1,客户ID:1,客户名称:Jordan Harrison,项目ID:1,项目名称:职责召唤,数量:0,总费用:0.0>

我没有了解为什么以下测试无效。我看过其他类似的问题,但是它们根本没有帮助我对测试的理解,因此我完全亏损了如何解决此问题。

这是测试:


public class OrderDAOTest {
    
    private final OrderDAO DAO = new OrderDAO();

@Before
    public void setup() {
        DBUtils.connect();
        DBUtils.getInstance().init("src/test/resources/sql-schema.sql", "src/test/resources/sql-data.sql");
    }

@Test
    public void testRead() {
        
        Long oId = 1L;
        Long iId = 1L;
        Long cId = 1L;
        String iName = "Call of Duty";
        double iCost = 25.99;
        Item item = new Item(iId, iName, iCost);
        Customer customer = new Customer(cId);
        CustomerDAO custDao = new CustomerDAO();
        customer = custDao.read(customer.getCustomerId());
        Order order = new Order();
        order.setOrderId(1L);
        order.setItem(item);
        order.setCustomer(customer);
        System.out.println(order);
        
        assertEquals(DAO.read(order.getOrderId()), order);
    }

这是read()使用order> ordereTemsfromresultset()方法,它在orderdao类中返回:

public Order orderItemsFromResultSet(ResultSet rs) throws SQLException {
        Long orderId = rs.getLong("fk_order_id");
        Long itemId = rs.getLong("item_id");
        String itemName = rs.getString("item_name");
        double itemCost = rs.getDouble("item_cost");
        Item item = new Item(itemId, itemName, itemCost);
        Order order = new Order(item, orderId);
        return order;
    }
    
    @Override
    public Order read(Long id) {
        try (Connection connection = DBUtils.getInstance().getConnection();
                PreparedStatement statement = connection.prepareStatement("SELECT * FROM order_items LEFT OUTER JOIN items ON items.item_id = order_items.fk_item_id WHERE fk_order_id = ?");) {
            statement.setLong(1, id);
            try (ResultSet resultSet = statement.executeQuery();) {
                resultSet.next();
                return orderItemsFromResultSet(resultSet);
            }
        } catch (Exception e) {
            LOGGER.debug(e);
            LOGGER.error(e.getMessage());
        }
        return null;
    }

我只是不了解它,需要在完全了解测试。

I am new to JUnit and testing and am seriously at my wits end with this error:
expected:<null> but was: expected:<null> but was:<Order ID: 1, Customer ID: 1, Customer Name: jordan harrison, Item ID: 1, Item Name: Call of Duty, Quantity: 0, Total Cost: 0.0>

I have no idea why the below test does not work whatsoever. I have looked at other similar questions but they don't help my understanding of the tests at all and left me at a complete loss for how to fix this.

Here's the test:


public class OrderDAOTest {
    
    private final OrderDAO DAO = new OrderDAO();

@Before
    public void setup() {
        DBUtils.connect();
        DBUtils.getInstance().init("src/test/resources/sql-schema.sql", "src/test/resources/sql-data.sql");
    }

@Test
    public void testRead() {
        
        Long oId = 1L;
        Long iId = 1L;
        Long cId = 1L;
        String iName = "Call of Duty";
        double iCost = 25.99;
        Item item = new Item(iId, iName, iCost);
        Customer customer = new Customer(cId);
        CustomerDAO custDao = new CustomerDAO();
        customer = custDao.read(customer.getCustomerId());
        Order order = new Order();
        order.setOrderId(1L);
        order.setItem(item);
        order.setCustomer(customer);
        System.out.println(order);
        
        assertEquals(DAO.read(order.getOrderId()), order);
    }

This is the read() method with the orderItemsFromResultSet() method that it returns in the OrderDAO Class:

public Order orderItemsFromResultSet(ResultSet rs) throws SQLException {
        Long orderId = rs.getLong("fk_order_id");
        Long itemId = rs.getLong("item_id");
        String itemName = rs.getString("item_name");
        double itemCost = rs.getDouble("item_cost");
        Item item = new Item(itemId, itemName, itemCost);
        Order order = new Order(item, orderId);
        return order;
    }
    
    @Override
    public Order read(Long id) {
        try (Connection connection = DBUtils.getInstance().getConnection();
                PreparedStatement statement = connection.prepareStatement("SELECT * FROM order_items LEFT OUTER JOIN items ON items.item_id = order_items.fk_item_id WHERE fk_order_id = ?");) {
            statement.setLong(1, id);
            try (ResultSet resultSet = statement.executeQuery();) {
                resultSet.next();
                return orderItemsFromResultSet(resultSet);
            }
        } catch (Exception e) {
            LOGGER.debug(e);
            LOGGER.error(e.getMessage());
        }
        return null;
    }

I just don't understand it and need help with understanding testing altogether.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩心不撩汉 2025-02-13 20:24:52

您正在捕获所有异常,然后只是返回null。有两个可能的地方,我会进一步调试。

  1. 检查连接是否正常。
  2. 检查查询执行是否也可以正常工作。在查询末尾卸下半龙,它可能起作用。

日志可能会在这里对您有所帮助。

You are catching all exceptions and then just returning null. There are two possible places I would debug further.

  1. Check if the Connection is working fine.
  2. Check if the query execution works fine too. Remove the semicolon at the end of the query and it may work.

The logs may help you here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文