有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何用JUnit测试这个类?

我必须为这个自动售货机类做单元测试。我开始思考如何做,但我意识到vending类没有返回类型的方法(顺便说一句,我只知道如何测试返回类型的方法),我一直使用assert

import java.util.Hashtable;

class VendingItem {
    double price;
    int numPieces;

    VendingItem(double price, int numPieces) {
        this.price = price;
        this.numPieces = numPieces;
    }

    void restock(int pieces) {
        this.numPieces = this.numPieces + pieces;
    }

    void purchase(int pieces) {
        this.numPieces = this.numPieces - pieces;
    }
}

/**
 * Class for a Vending Machine. Contains a hashtable mapping item names to item
 * data, as well as the current balance of money that has been deposited into
 * the machine.
 */
public class Vending {
    private static Hashtable<String, VendingItem> Stock = new Hashtable<String, VendingItem>();
    private double balance;

    Vending(int numCandy, int numGum) {
        Stock.put("Candy", new VendingItem(1.25, numCandy));
        Stock.put("Gum", new VendingItem(.5, numGum));
        this.balance = 0;
    }

    /** resets the Balance to 0 */
    void resetBalance() {
        this.balance = 0;
    }

    /** returns the current balance */
    double getBalance() {
        return this.balance;
    }

    /**
     * adds money to the machine's balance
     * 
     * @param amt
     *            how much money to add
     */
    void addMoney(double amt) {
        this.balance = this.balance + amt;
    }

    /**
     * attempt to purchase named item. Message returned if the balance isn't
     * sufficient to cover the item cost.
     * 
     * @param name
     *            The name of the item to purchase ("Candy" or "Gum")
     */
    void select(String name) {
        if (Stock.containsKey(name)) {
            VendingItem item = Stock.get(name);
            if (balance >= item.price) {
                item.purchase(1);
                this.balance = this.balance - item.price;
            } else
                System.out.println("Gimme more money");
        } else
            System.out.println("Sorry, don't know that item");
    }

}

例如,你认为我如何测试打印东西的方法


共 (4) 个答案

  1. # 1 楼答案

    那么这个呢:

    @Test
    public void testResetVendingBalance()  {
        Vending vending = new Vending(0,0);
        vending.addMoney(7);
        vending.resetBalance();
        assertEquals("Test if vending reset.",0, vending.getBalance(), 0);
    }
    
    @Test
    public void testAddVendingBalance()  {
        Vending vending = new Vending(0,0);
        vending.addMoney(7);
        assertEquals("Test money amount.",7, vending.getBalance(), 0);
    }
    
  2. # 2 楼答案

    你应该问自己“我到底想测试什么?”在你的例子中,答案可能是“重新进货机器会增加数量,而购买会减少数量”。也许价格不变的测试也值得一试

    既然你已经知道了,问题是你需要什么来测试这个?为你的股票和价格买单在这里可能会很好

  3. # 3 楼答案

    重新分配系统。有什么可以测试的吗? 从系统的javadoc:

    static void setOut(PrintStream out)
    Reassigns the "standard" output stream.

    然后你可以验证它的价值

  4. # 4 楼答案

    在大多数情况下,你应该测试逻辑。例如,您重置余额并检查其是否等于0,或者获取余额并保持其值,然后使用addMoney方法检查余额是否具有预期值。所有这些都可以通过assert方法完成。我希望这些解释有所帮助