junit 我的J单元测试一直通过,尽管代码中存在故意的错误

wvt8vs2t  于 2022-11-11  发布在  其他
关注(0)|答案(1)|浏览(133)

这是我要测试的方法。我为这个方法写了一个测试,它通过了。但是,我故意插入了一个错误,测试仍然通过了。

public void sell (User myUser, String item) {
    if(myUser.getRole() == Roles.CASHIER) {
        for(Product inventory : inventories) {
            if(inventory.getName().equals(item) && inventory.getPrice() <= myUser.getBalance() ) {
                System.out.println("sale successful");
                inventories.remove(inventory);
            }
            else {
                System.out.println(item +  " is out of stock!");
            }
        }
    } else {
        System.out.println("Staff member can't purchase goods");
    }
}

这是我写的测试,尽管方法中有故意的错误,但它通过了所有的案例。我是J单元的新手,所以我甚至不确定我做得对不对。

@Test
    void reStock() {
    if(myUser.getRole() == Roles.CASHIER) {
        Assertions.assertEquals(myUser.getRole(), Roles.CASHIER);
        Assertions.assertNotNull(inventories, "Store stock must not be empty");
        for(Product inventory : inventories) {
            if(inventory.getName().equals(item) && inventory.getPrice() <= myUser.getBalance() ) {
                Assertions.assertEquals(inventory.getName(), item);
                Assertions.assertTrue(inventory.getPrice() <= myUser.getBalance(), "Balance should not be less than the price of the item");
                int len = inventories.size();
                inventories.remove(inventory);
                Assertions.assertNotEquals(len, (len - 1));
            }
            else {
                System.out.println(item +  " is out of stock!");
            }
        }
    }
    else {
        System.out.println("Staff member can't purchase goods");
    }

}
kjthegm6

kjthegm61#

如果您从不抛出异常,则您的测试将永远不会失败:)
这是我会重构你的代码:

class Shop {
 public void sell(User myUser, String item) {
    if(myUser.getRole() == Roles.CASHIER) {
        for(Product inventory : inventories) {
            if(inventory.getName().equals(item) && inventory.getPrice() <= myUser.getBalance() ) {
                System.out.println("sale successful");
                inventories.remove(inventory);
            }
            else {
                throw new OutOfStockException(item);
            }
        }
    } else {
        throw new CantSellException(myUser);
    }
  }
}

现在我们可以测试一个有效的销售它:

@Test
public void testValidSale() {
   User cashier = new User(Roles.CASHIER); // Create a valid User
   List<String> inventories = new ArrayList<>();
   inventories.add("Vodka");
   inventories.add("Beer");
   inventories.add("Rum");
   Shop shop = new Shop();
   shop.sell(cashier, "Vodka"); // An item that exists
   assertEquals(inventories.size(), 2);
   assertFalse(inventories.contains("Vodka"));
   // You should assert that inventories contains exactly "Beer" and "Rum"
}

@Test
public void testOutOfStockException() {
   User cashier = new User(Roles.CASHIER); // Create a valid User
   List<String> inventories = new ArrayList<>();
   inventories.add("Vodka");
   inventories.add("Beer");
   inventories.add("Rum");
   Shop shop = new Shop();
   try {
       shop.sell("whiskey"); // An item that's not in the inventory
       Assert.fail("We expect an exception when the item is out of stock");
   }
   catch(OutOfStockException e) {
       // Nothing to do: you can test that the error message is correct
       // if you want to
   }
}

public  void testUserCannotSell() {
  User customer = new User(Roles.CUSTOMER); // A user that cannot sell
   List<String> inventories = new ArrayList<>();
   inventories.add("Vodka");
   Shop shop = new Shop();
   try {
       // This item is not in the inventory
       // but the user cannot sell anything so the other exception should
       // happen first
        shop.sell("whiskey"); 
       Assert.fail("We expect an exception when the item is out of stock");
   }
   catch(CantSellException e) {
       // Nothing to do: you can test that the error message is correct
       // if you want to
   }

}

这段代码并不优雅,但它应该给予您了解测试方法的意义。

相关问题