$25
1. Introduction
This assignment helps you practice Test-Driven Development
(TDD). The problem you are trying to solve here is one that you should be able to solve in a prior course on Data Structures. However, here you need to use the TDD methodology we have covered. Note that the 3 laws of TDD are:
1. You may not write production code until you have written a failing unit test
2. You may not write more of a unit test than is sufficient to fail
3. You may not write more production code than is sufficient to pass the currently failing test
NOTE:: do not write the functional code in your head before you write the test.
In this assignment, you will use JUnit and Java to implement a
bounded stack of size 5, namely MyStack, using an integer array. Recall the three basic operations of a traditional stack are push, pop, and top as shown below:
2. Get Started with Basic Test
You should start the assignment by creating a JUnit test class and name it “BasicTest”. This class is for testing/implementing basic operations of a bounded stack. Note that the data structure you should use is array. The following test cases are required, although you do not have to follow the exact order when you implement them. Also, you can have more test cases than what are listed here. 1. Create a MyStack and verify that IsEmpty is true.
2. Push a number on the MyStack and verify that IsEmpty is false.
3. Push a number, Pop the number, and verify that IsEmpty is true.
4. Push a number, remember what it is; Pop the number, and verify that the two numbers are equal.
5. Push three numbers, remember what they are; Pop each one, andverify that they are removed in the correct order. 6. Pop a MyStack that has no elements.
7. Push a number and then call Top. Verify that IsEmpty is false.
8. Push a number, remember what it is; and then call Top. Verifythat the number that is returned is the same as the one that was pushed.
When you implement these tests the MyStack class should also be implemented along the way. To indicate the order in which you implement the tests and features label each method (in both test and functional code) using STEP 1, STEP 2, etc, and include a short description about the method. Listing 1 shows a sample based on the class demo.
In addition, you should also document your refactor activities in a text file refactor.txt. Each line of this file should look like the following: Refactor after STEP 2: “description of changes.”
Note that you do not have to do refactor in every single “redgreen-refactor" cycle, but for the ones that you do improve you should document the improvement in this file.
3. Advanced Test
Now that you have already finished implementing a bounded stack using TDD, you can write more tests for some special cases: 1. Call Top on a MyStack with no elements.
2. Push a number to a MyStack that is full (hint: you may throw an exception here).
3. A self-defined, new test case (it can be anything, e.g., performance test).
4. Aggregating Tests in Suite
Now that you have two test classes, let’s combine them and make a test suite. When you run this test suite all test cases in both classes should be executed automatically (hint: to do this, you need to use the @RunWith annotation as seen in Step 9 of the JUnit guide and demo code).