new to Java programming, I am just trying to understand how I can make this class be tested. I have made a Queue class:
public class Queue<E> {
private ArrayList<E> items;
public Queue(){
items = new ArrayList<E>();
}
public void join(E e) {
items.add(e);
}
public E leave() throws EmptyQueueError {
if (items.isEmpty()) {
throw new EmptyQueueError();
}
return items.remove(0);
}
}
I want to make a JUnit called QueueTest that is automatically reset to empty before each test that is commenced? Then I want it check that removing an item from an empty queue throws an EmptyQueueError? Finally, I want it to check that when several items join an (initially) empty queue, it is the first item that joined which is the first to leave?
It’s a tutorial I am following but it fails to make me understand. I have made the class above and I have attempted the JTest class:
package queue;
public class QueueTest<e> {
private Queue q;
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void test() {
q = new Queue<e>();
assertEquals("Empty Queue", 0, q);
}
}
Am I close to what I am trying to achieve? I am trying to do the first one.
Thank you for your help and ample time.
You’re doing well.
The problem with this is that you make a new Queue instance, but you don’t do anything with it.
Then your test’s innards could be
What this means is that you’d make a Queue (‘A’), then immediately try to get an item (‘B’). This should cause an exception. If it does not, your test fails with “exception expected” (‘C’). If the expected exception is thrown, you code works (‘D’)
I’ll post more once you get this working.