【问题标题】:Is this a valid unit test for Hash Map? [duplicate]这是 Hash Map 的有效单元测试吗? [复制]
【发布时间】:2019-06-29 11:04:35
【问题描述】:

如果我以 write 方式编写单元测试,我试图理解。我有一个哈希图,用于存储我的客户注册。我正在尝试为我的 createCustomer 方法编写单元测试。如果我的方向正确,有人可以给我指点吗?

void addCustomer () {
        System.out.println ();

        String customerName = getString ("Enter Customer Name with cappital letar: ");

        String customerAddress = getString ("Enter Customer Address with cappital letar: ");

        int customerPhone = getInt ("Enter Customer phone:");

        int customerID = checkID ();
        Customer customer = new Customer (customerName, customerAddress, customerID, customerPhone);
        customerList.put (customerID, customer);
        System.out.println ("Customer Added");

    }

@Test
    public void addCustomerTest () {
        HashMap<Integer,Customer> customerList = new HashMap<> ();
        String customerName = "Anna";
        String customerAddress = "London";
        int customerPhone =  1010101;

        int customerID = 1000;
        Customer customer = new Customer (customerName, customerAddress, customerID, customerPhone);
        customerList.put (customerID, customer);

        assertTrue(customerList.containsKey(customerID) && customerList.get(customerID) != null);

    }

【问题讨论】:

  • 这对于 SO 来说几乎是题外话,但是您的测试暴露了代码中的设计问题。您正在混合 UI,您从用户那里获取多个值,然后创建一个具有此状态的对象,然后将其保存到某个全局位置。单元测试只会测试 Customer 对象的创建,您可以在其中断言正确允许使用 null 或无效参数。

标签: java unit-testing junit4


【解决方案1】:

当前您对此类进行单元测试时,您不是 HashMap 作者。
所以不,你没有以正确的方式测试你的代码。
您想要的单元测试是 your 类的 API:即addCustomer()
Map 是一个实现细节,它可能会随着时间而改变,并且您不想测试。

您的单元测试应如下所示:

@Test
public void addCustomer() {
    CustomerRepository repo = new CustomerRepository();
    String customerName = "Anna";
    String customerAddress = "London";
    int customerPhone =  1010101;
    int customerID = 1000;
    // Mock the System IN to read these values
    // ...
    // invoke the method under test
    repo.addCustomer();
    // assert that the repo contains the new object
    Customer actual = repo.findCustomerById(customerID);
    assertNotNull(actual);
    assertEquals(customerName, actual.getCustomerName());
    assertEquals(customerID, actual.getCustomerID());
    // and so for for each field
}

【讨论】:

    【解决方案2】:

    当您编写单元测试时,您是在测试您编写的代码单元

    在测试中

    @Test
    public void addCustomerTest () {
        HashMap<Integer,Customer> customerList = new HashMap<> ();
        String customerName = "Anna";
        String customerAddress = "London";
        int customerPhone =  1010101;
    
        int customerID = 1000;
        Customer customer = new Customer (customerName, customerAddress, customerID, customerPhone);
        customerList.put (customerID, customer);
    
        assertTrue(customerList.containsKey(customerID) && customerList.get(customerID) != null);
    
    }
    

    您不是在测试您的代码,但最终您是在测试HashMap

    要编写好的单元测试,您需要:

    • 确定您编写并希望测试的方法
    • 如果您的方法接受参数标识边界值,这些参数可能会给您的代码带来问题(例如,对象的空值、列表的空列表、最大或最小整数整数)
    • 编写测试以检查您的代码是否适用于这些特殊值
    • 编写一个测试以检查它是否适用于正常值

    如果一切正常,您可以尝试重写代码重构它以获得更好的设计,遵循mantra

    • RED - 想想你想开发什么
    • 绿色 - 思考如何让您的测试通过
    • 重构 - 思考如何改进现有实施

    然后添加新的测试并再次遵循红色、绿色、重构的过程。

    【讨论】:

    • 所以我应该写: hashmap.size() +1 等于 hashmap.size ?对吗?
    猜你喜欢
    • 2011-11-23
    • 2017-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-13
    • 2019-11-03
    相关资源
    最近更新 更多