Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Unit Testing in Java What to Test Test the running total

Alison Brodribb
Alison Brodribb
24,615 Points

Test not importing package or junit

I'm trying to do this test, however it has all of these errors from the base code:

I can't see how to fix the issue, as it doesn't seem to be in either of the files in the quiz. Any help?

JavaTester.java:69: error: illegal start of expression package com.teamtreehouse.vending; ^ JavaTester.java:69: error: not a statement package com.teamtreehouse.vending; ^ JavaTester.java:71: error: illegal start of expression import org.junit.Before; ^ JavaTester.java:71: error: not a statement import org.junit.Before; ^ JavaTester.java:72: error: illegal start of expression import org.junit.Test; ^ JavaTester.java:72: error: not a statement import org.junit.Test; ^ JavaTester.java:74: error: illegal start of expression import static org.junit.Assert.; ^ JavaTester.java:74: error: illegal start of expression import static org.junit.Assert.; ^ JavaTester.java:74: error: ';' expected import static org.junit.Assert.; ^ JavaTester.java:74: error: expected import static org.junit.Assert.; ^ JavaTester.java:74: error: illegal start of expression import static org.junit.Assert.*; ^ JavaTester.java:76: error: illegal start of expression public class VendingMachineTest { ^

com/teamtreehouse/vending/VendingMachineTest.java
package com.teamtreehouse.vending;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

public class VendingMachineTest {
    private VendingMachine machine;

    public class NotifierSub implements Notifier {

        @Override
        public void onSale(Item item) {
            return;
        }
    }

    @Before
    public void setUp() throws Exception {
        Notifier notifier = new NotifierSub();
        machine = new VendingMachine(notifier, 10, 10, 10);
        machine.restock("A1", "Twinkies", 10, 30, 75);
    }

      @Test
    public void vendingWhenStockedReturnsItem() throws Exception {
        machine.addMoney(75);

        Item item = machine.vend("A1");

        assertEquals(75, machine.getRunningSalesTotal());
    }

    @Test
    public void vendingWhenStockedReturnsItem() throws Exception {
        machine.addMoney(75);

        Item item = machine.vend("A1");

        assertEquals("Twinkies", item.getName());
    }
}
com/teamtreehouse/vending/VendingMachine.java
package com.teamtreehouse.vending;


public class VendingMachine {
    private final Notifier notifier;
    private final AbstractChooser chooser;
    private final Creditor creditor;
    private final Bin[][] bins;
    private int runningSalesTotal;

    public VendingMachine(Notifier notifier, int rowCount, int columnCount, int maxItemsPerBin) {
        this.notifier = notifier;
        chooser = new AlphaNumericChooser(rowCount, columnCount);
        creditor = new Creditor();
        runningSalesTotal = 0;
        bins = new Bin[rowCount][columnCount];
        for (int row = 0; row < rowCount; row++) {
            for (int col = 0; col < columnCount; col++) {
                bins[row][col] = new Bin(maxItemsPerBin);
            }
        }
    }

    public void addMoney(int money) {
        creditor.addFunds(money);
    }

    public int refundMoney() {
        return creditor.refund();
    }

    public Item vend(String input) throws InvalidLocationException, NotEnoughFundsException {
        Bin bin = binByInput(input);
        int price = bin.getItemPrice();
        creditor.deduct(price);
        runningSalesTotal += price;
        Item item = bin.release();
        notifier.onSale(item);
        return item;
    }

    public int getRunningSalesTotal() {
        return runningSalesTotal;
    }

    public void restock(String input, String name, int amount, int wholesalePrice, int retailPrice) throws InvalidLocationException {
        Bin bin = binByInput(input);
        bin.restock(name, amount, wholesalePrice, retailPrice);
    }

    private Bin binByInput(String input) throws InvalidLocationException {
        AbstractChooser.Location location = chooser.locationFromInput(input);
        return bins[location.getRow()][location.getColumn()];
    }

}

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! There currently seems to be a problem with Java challenges across the board. Another moderator and I have reported it to Treehouse staff. I hope they get it fixed soon! :sparkles: