2

I have to do unit tests for this vending class. I started thinking how i could do them but i realized that the vending class doesn't have a method with a return type(btw i just know how to test methods with return type), and i always have used 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");
    }

}

How you think i could test the methods that print something, for example?.

Lu MON
  • 107
  • 8
  • You can try adding Getters for the variables and in the JUnits make every method and then print the getters for each variable? – Alejandro Cortes Sep 11 '16 at 20:04
  • You can still test void methods if they have side effects (like changing balance or numPieces). You can check if they changed after your method call with a getter (see @AlejandroCortes comment). For the System.out.print testing, see http://stackoverflow.com/questions/1119385/junit-test-for-system-out-println. – J. Kamans Sep 11 '16 at 20:09

4 Answers4

2

In most cases you should test the logic. For example you reset the balance and check is it equals to 0 or get the balance and keep it's value then use addMoney method and check is the balance have the expected value. And all of these can be done through assert methods. I hope that these explanations have helped.

M.Amini
  • 106
  • 1
  • 6
2

what about this:

@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);
}
lidox
  • 1,901
  • 3
  • 21
  • 40
1

Reassign System.out to something you can test? From the javadoc of System:

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

Then you can verify it's value.

Koos Gadellaa
  • 1,220
  • 7
  • 17
0

The question you should ask yourself is "what exactly is it do I want to test?" In your case, the answer is probably something like "restocking the machine adds to the quantity, and purchasing subtracts from the quantity". Perhaps a test that the price doesn't change is worth having as well.

Now that you have that, the question is what do you need in order to test this? Getters for your stock and price might be good here.

Joe C
  • 15,324
  • 8
  • 38
  • 50