Getting Familiar with Eclipse, Queues, Stacks and Unit Tests

Goals

Unit Testing is a method of testing source code that verifies that individual units are working properly. A unit of code refers to the smallest testable part of an application, in Java this corresponds to a method. JUnit is a Java package that implements unit testing. Eclipse provides grate tools to automate the creation of JUnit tests as well as provide a good interface to running the tests. Together these two tools make systematically testing your program very easy. In this lab we'll learn how to do this.

Creating JUnit Tests

We'll be learning how to run JUnit tests on Eclipse. We've provided you with code that you must download from the class website to a new directory. There is a queue Interface, MyQueueI, a good implementation of a queue, MyGoodQueue, and a very bad implementation of a queue, MyBadQueue. MyBadQueue is written using two stacks.

Start by creating a new Java project in Eclipse from existing source:

  1. Open Eclipse
  2. File->New->Java Project
  3. Name the project
  4. Select the "Create Project from Existing Source" radio button and choose the directory where you downloaded the code
  5. Hit Finish

Creating JUnit Tests

JUnit is a very powerful package for creating unit tests to ensure your code is working correctly. An extension to JUnit allows us to partly automate the grading of your programming assignments.

We are first going to create a JUnit test to check if MyGoodQueue and MyBadQueue work correctly (you can guess which one doesn't work). MyGoodQueue uses the java.util.LinkedList to implement the queue. MyBadQueue uses java.util.Stack to implement the queue.

Create a JUnit to test the interface MyQueueI. Your junit must individually test all 4 methods described in the interface. Remember each test is independent, i.e. tests are not cumulative.

  1. In the Package Explorer right click on MyQueueI->New->JUnit Test Case
  2. Choose the radio box for "New JUnit 4 test" and check the box next to setUp(). Click Next
  3. Check the box for each of the methods you would like to test. In this case all 4. Click Finish
  4. If asked to add JUnit 4 to the build path choose "Perform the following action"
  5. Fill in each of the 4 tests appropriately. Make sure to remove the fail() in each test and assert it correctly, see Assert Types for a list of possible asserts.

The setUp() method will be run before each test is run allowing you to initialize your structures here. Use setUp() to first test the MyGoodeQueue implementation. To run the test go to the Run menu->Run As->JUnit Test

Once you are satisfied that it passes all tests change the code in setUp() to test the MyBadQueue.

Where's the error in MyBadQueue?

Feel free to ask your TA questions if at any step you are stuck. If you would like additional reference please follow this tutorial to create your JUnit tests.

Important: You may notice that some of these tests seem pointless because you must use other methods (of which you cannot guarantee the correctness of either) to test. For the sake of a simple introduction to JUnit we will ignore this important detail, but in general, this is a complicated and important part of JUnit testing.


Debugging in Eclipse

So you've just realized that MyBadQueue doesn't work. The code is pretty convoluted but luckily you can uses the powerful Eclipse debugger.

The easiest way to debug in Eclipse is to:
  1. Create a main method in MyBadQueue. Fill in code that will exercise each of the four methods.
  2. Create breakpoints in your code where you think there may be a problem. To create a breakpoint, double click on the bar to the left of the line where you want the breakpoint. A small circle should appear.
  3. After you have created breakpoints at points in your code where you would like to analyze the state of the java program, change your perspective to "debug" (Window->Open Perspective->Other->Debug
  4. Run the program in debug mode (Run->Debug), and when it hits the breakpoint, you can step through the program and "look into" different objects.

After You've Debugged

Fix MyBadQueue so that it passes the JUnit tests that you've written.