Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Unit Testing in Java What to Test Ruler of rules

praveen gudimetla
praveen gudimetla
464 Points

Okay, so now I'd like to upgrade that previous test we wrote restockingWithDifferentItemsIsNotAllowed to check that we a

the challenge says

Okay, so now I'd like to upgrade that previous test we wrote restockingWithDifferentItemsIsNotAllowed to check that we are getting the appropriate error message.

PS. If you are stuck, refresh your memory with the pattern used in overstockingNotAllowed .

I am not sure what needs to be done on this challenge? please advise package com.teamtreehouse.vending;

import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException;

import static org.junit.Assert.*;

public class BinTest {

private Bin bin ;

@Rule
public ExpectedException thrown = ExpectedException.none();


@Before
public void setUp() throws Exception {
    bin = new Bin(10);
}

@Test(expected = IllegalArgumentException.class)
public void restockingWithDifferentItemsIsNotAllowed() throws Exception {
    bin.restock("Cheetos", 1, 100, 50);
    bin.restock("Doritos", 1, 100, 50);
}

@Test
public void whenEmptyPriceIsZero() throws Exception {
    assertEquals(0, bin.getItemPrice());
}

@Test
public void whenEmptyNameIsNull() throws Exception {
    assertNull(bin.getItemName());
}

@Test
public void overstockingNotAllowed() throws Exception {
    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("There are only 10 spots left");

    bin.restock("Fritos", 2600, 100, 50);
}

}

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

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.junit.Assert.*;

public class BinTest {

    private Bin bin ;

    @Rule
    public ExpectedException thrown = ExpectedException.none();


    @Before
    public void setUp() throws Exception {
        bin = new Bin(10);
    }

    @Test(expected = IllegalArgumentException.class)
    public void restockingWithDifferentItemsIsNotAllowed() throws Exception {
        bin.restock("Cheetos", 1, 100, 50);
        bin.restock("Doritos", 1, 100, 50);
    }

    @Test
    public void whenEmptyPriceIsZero() throws Exception {
        assertEquals(0, bin.getItemPrice());
    }

    @Test
    public void whenEmptyNameIsNull() throws Exception {
        assertNull(bin.getItemName());
    }

    @Test
    public void overstockingNotAllowed() throws Exception {
        thrown.expect(IllegalArgumentException.class);
        thrown.expectMessage("There are only 10 spots left");

        bin.restock("Fritos", 2600, 100, 50);
    }
}
com/teamtreehouse/vending/Bin.java
package com.teamtreehouse.vending;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class Bin {
    private final BlockingQueue<Item> items;

    public Bin(int maxItems) {
        items = new ArrayBlockingQueue<>(maxItems);
    }

    public boolean isEmpty() {
        return items.isEmpty();
    }

    public int getAvailableSlots() {
        return items.remainingCapacity();
    }

    public String getItemName() {
        if (isEmpty()) return null;
        return items.peek().getName();
    }

    public int getItemPrice() {
        if (isEmpty()) {
            return 0;
        }
        return items.peek().getRetailPrice();
    }

    public Item release() {
        return items.poll();
    }

    public void restock(String name, int amount, int wholesalePrice, int retailPrice) {
        if (!isEmpty() && !name.equalsIgnoreCase(getItemName())) {
            throw new IllegalArgumentException(String.format("Cannot restock %s with %s", getItemName(), name));
        }
        if (amount > getAvailableSlots()) {
            throw new IllegalArgumentException(String.format("There are only %d spots left", getAvailableSlots()));
        }
        for (int i = 0; i < amount; i++) {
            items.add(new Item(name, wholesalePrice, retailPrice));
        }
    }

}
com/teamtreehouse/vending/Item.java
package com.teamtreehouse.vending;

public class Item {
    private final String name;
    private final int wholesalePrice;
    private final int retailPrice;

    public Item(String name, int wholesalePrice, int retailPrice) {
        this.name = name;
        this.wholesalePrice = wholesalePrice;
        this.retailPrice = retailPrice;
    }

    public String getName() {
        return name;
    }

    public int getWholesalePrice() {
        return wholesalePrice;
    }

    public int getRetailPrice() {
        return retailPrice;
    }
}

Hello,

The first thing you need to do is change

@Test(expected = IllegalArgumentException.class)
public void restockingWithDifferentItemsIsNotAllowed() throws Exception {
    bin.restock("Cheetos", 1, 100, 50);
    bin.restock("Doritos", 1, 100, 50);
}

to this:

@Test
public void restockingWithDifferentItemsIsNotAllowed() throws Exception {
    bin.restock("Cheetos", 1, 100, 50);
    bin.restock("Doritos", 1, 100, 50);
}

Then you will see the error message:

FAILURES

restockingWithDifferentItemsIsNotAllowed: java.lang.IllegalArgumentException: Cannot restock Cheetos with Doritos

With that error message you can complete the challenge

@Test
public void restockingWithDifferentItemsIsNotAllowed() throws Exception {
    bin.restock("Cheetos", 1, 100, 50);
    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("Cannot restock Cheetos with Doritos");
    bin.restock("Doritos", 1, 100, 50);
}