7. Using JUnit with Eclipse

This practice will introduce you to use the JUnit Framework integrated with the Eclipse IDE. Be sure you have the latest version of JUnit plugin installed (you can download it from the site http://www.junit.org).

Step 1 - Writing the code

First of all, create a new project in the Eclipse IDE and add to it the two classes: ShoppingCart and ShoppingItem. You should understand the structure of these classes and complete the methods with the comment TODO on it. The objective is to simulate a simple e-commerce's shopping cart and to test the integrity of the code you've wrote.

Step 2 - Creating the test

In the Eclipse IDE, create a new test case from the menu File -> New -> JUnit Test Case. Give the name ShoppingTest to it, check the boxes to create the setUp() and tearDown() methods and choose ShoppingCart to be the class under test. Choose Next. Now you can select the methods for which test method stubs should be created. Choose the methods addItem(), removeItem(), removeAllItems() and then click Finish.

Step 3 - Writing the test

In the testAddItem() method of the class you've created, write the following code:

ShoppingCart cart = new ShoppingCart();
ShoppingItem item1 = new ShoppingItem(1, "item 1", 80.0, 10.0);
ShoppingItem item2 = new ShoppingItem(2, "item 2", 110.0, 15.0);

assertTrue(cart.addItem(item1));
assertTrue(cart.addItem(item2));

assertEquals(cart.getTotal(), 190.0, 0.0);
assertEquals(cart.getDiscount(), 24.5, 0.0);
assertEquals(cart.getTotalWithDiscount(), 165.5, 0.0);
assertEquals(cart.getNumberOfItems(), 2);

assertFalse(cart.addItem(item1));
assertFalse(cart.addItem(null));

What this code is testing? Write down your answers. If you detect a failure in your code, do not correct it yet.

Now, copy the following code to the testRemoveItem() method:

ShoppingCart cart = new ShoppingCart();
ShoppingItem item1 = new ShoppingItem(1, "item 1", 80.0, 10.0);
ShoppingItem item2 = new ShoppingItem(2, "item 2", 110.0, 15.0);

cart.addItem(item1);
cart.addItem(item2);

assertTrue(cart.removeItem(item1));

assertEquals(cart.getTotal(), 110.0, 0.0);
assertEquals(cart.getDiscount(), 16.5, 0.0);
assertEquals(cart.getTotalWithDiscount(), 93.5, 0.0);
assertEquals(cart.getNumberOfItems(), 1);

assertFalse(cart.removeItem(item1));
assertFalse(cart.removeItem(null));

What this code is testing? Write down your answers.

Now, in the testRemoveAllItems(), copy the code below:

ShoppingCart cart = new ShoppingCart();
ShoppingItem item1 = new ShoppingItem(1, "item 1", 80.0, 10.0);
ShoppingItem item2 = new ShoppingItem(2, "item 2", 110.0, 15.0);

cart.addItem(item1);
cart.addItem(item2);

assertTrue(cart.removeAllItems());

assertEquals(cart.getTotal(), 0.0, 0.0);
assertEquals(cart.getDiscount(), 0.0, 0.0);
assertEquals(cart.getTotalWithDiscount(), 0.0, 0.0);
assertEquals(cart.getNumberOfItems(), 0);

What this code is testing?

Step 4 - Running the test

Finally, you can run the tests. In the Eclipse menu, click on Run -> Run As -> JUnit Test. You'll see the JUnit graphics interface integrated in the IDE. Check the results. If there are errors or failures, correct your code and rerun the tests.

Step 5 - Refining the test

Identify the fixture for the test case ShoppingTest and write the setUp() and tearDown() methods. Rerun the test to check if there are any error.

Hand in all material you've created.