Starting from:

$30

CS1632-Exercise 2 Simulating the Main Rent-A-Cat Rental System Software Solved

In this exercise, we will simulate the main Rent-A-Cat rental system software.  This is obviously a "toy" implementation of the vast and powerful Rent-A-Cat apparatus.

I have created some skeleton code for you to fill in for this exercise.  It is up to you to fill in the `returnCat()`, `rentCat()`, `listCats()` and `catExists()` methods, and write unit tests for them.  Unit tests must use doubles for the Cat object with appropriate stubbing.  I have intentionally inserted a defect on every Cat method such that an exception is fired if you try to use a real Cat object in any way during your unit testing!  Those defects are turned off when Cat is used within the main RentACat program.

Rent-A-Cat rents cats to customers for various needs (mousing, companionship, homework help, etc.).  From the main menu, users may:

1. See list of cats for rent
2. Rent a cat to a customer
3. Return a cat
4. Quit

A cat which is out for rental cannot be rented and will not be listed until it has been returned.  We will not charge money for this exercise.

## Sample Output

```
Option [1,2,3,4] 1
Cats for Rent
ID 1. Jennyanydots
ID 2. Old Deuteronomy
ID 3. Mistoffelees
Option [1,2,3,4] 2
Rent which cat? 1
Jennyanydots has been rented.
Option [1,2,3,4] 1
Cats for Rent
ID 2. Old Deuteronomy
ID 3. Mistoffelees
Option [1,2,3,4] 2
Rent which cat? 1
Sorry, Jennanydots is not here!
Rent which cat? 7
Invalid cat ID.
Rent which cat? 3
Mistoffelees has been rented.
Option [1,2,3,4] 1
Cats for Rent
ID 2. Old Deuteronomy
Option [1,2,3,4] 3
Return which cat? 7
Invalid cat ID.  
Return which cat? Jennyanydots
Invalid cat ID.
Return which cat? 1
Welcome back, Jennyanydots!
Option [1,2,3,4] 1
Cats for Rent
ID 1. Jennyanydots
ID 2. Old Deuteronomy
Option [1,2,3,4] 4
Closing up shop for the day!
```

You will modify two classes: **RentACatImpl.java** and **RentACatTest.java**.  The RentACatImpl class is an (incomplete) implementation of the Rent-A-Cat system.  The RentACatTest class is a JUnit unit test class that tests RentACatImpl.  All locations where you should add code is marked with // TODO comments.

We are going to use the TestRunner class to invoke JUnit on the RentACatTest class.  Note how the RentACatTest.class is added to the list of classesToTest.

You should use test doubles/mocks for any references to classes other than the one under test that the tested class is dependent upon (i.e. you need to mock any Cat objects).  You do not need to mock the ArrayList class used within RentACatImpl even though RentACatImpl is dependent upon it.  ArrayList is a Java standard library class so we will assume that it is fully tested and defect-free at this point. :)

## Running Unit Tests

1. First let's do a sanity check to see if JUnit works well with the Java version installed on your machine.  For Windows try doing:
    ```
    run.bat
    ```
    For Mac or Linux, try doing:
    ```
    bash run.sh
    ```
    For those of you who prefer Makefiles, you can also do:
    ```
    make
    ```
    If successful, you will get a message "ALL TESTS PASSSED".  But hold your horses, we aren't done yet!  The tests passed because they are currently empty.
    
2. Now you are ready to fill in the test cases in RentACatTest.  If you want to do a sanity test, try a very simple assertion that always succeeds in testGetCatNullNumCats0:
    ```
    assertTrue(true);
    ```
    Now you see the message "ALL TESTS PASSED" again.  Yes!  Now let's try an assertion that fails.  Change the above to:
    ```
    assertFalse(true);
    ```
    Now you should see a test failure like the below:
    ```
    testGetCatNullNumCats0(RentACatTest): null

    !!! - At least one failure, see above.
    ```
    What does that null mean?  It just means you didn't supply a failure message.  Try the following:
    ```
    assertFalse("True is not false", true);
    ```
    Then you should get:
    ```
    testGetCatNullNumCats0(RentACatTest): True is not false

    !!! - At least one failure, see above.
    ```
    
3. Now you are ready to start writing the RentACatTest class for real.  Start by adding very simple tests to gain confidence.  Next, try adding more complex cases that require Cat objects.  For that, you will have to modify setUp() to create some Cat test doubles with proper stubs.  We learned how to do that in class.  If you are still unsure, look at the [LinkedListUnitTest.java](https://github.com/wonsunahn/CS1632_Summer2020/blob/master/sample_code/junit_example/LinkedListUnitTest.java) sample code or the NoogieTest and CoogieTest under the Example/ directory.

## Tips

1. We will try to apply the Test Driven Development (TDD) model here.  Try writing the test case(s) FIRST before writing the code for a feature.  This way, you will always have 100% test coverage for the code you have written and are writing.  Hence, if you break any part of it in the course of adding a feature or refactoring your code, you will know immediately.  Otherwise, if you test at the very end, you may have to do some major changes.
1. Remember to _not_ mock the class under test (i.e. RentACat), only external classes that it depends upon (i.e. Cat).  In fact, if you don't mock Cat and use the actual Cat objects, your tests will most likely fail.  I have injected artificial defects in the form of exceptions into the Cat class to emulate a buggy Cat class.  Your tests should work regardless of what's inside Cat.
1. The easiest thing to do is assert against a return value, but you can also assert against attributes of an object.  For example:
    ```
    @Test
    public void testCatName() {
       assertEquals("Expected name", cat.getName());
    }
    ```
    You can also use the Mockito verify method to perform behavior verification.
1. Make use of the @Before and @After methods in your JUnit testing.  @Before and @After methods are invoked before and after each @Test method.  They are used to set up and tear down test fixtures.  Test fixtures in JUnit are objects that need to be created and initialized before performing each test case.  You can think of them as "actors" in your test script.  Having the @Before and @After allows us to avoid duplicating test fixture code in each test case.
1. In RentACatTest.java, pay close attention to the Javadoc comments on top of each @Test method which describe the preconditions, execution steps, and postconditions.  Remember, part of the preconditions may be already fulfilled with the test fixture initialized in the @Before method.
1. Each @Test method represents a test case.  A JUnit class with one or more @Test methods represents a test plan. A JUnit class is usually named after whichever class it is testing, with the string `Test` appended to the tested class name.  For example, Foo.java would be tested by FooTest.java.  But this is not necessarily the case.  A JUnit class may test multiple classes with related functionality; in that case, it is typically named after the functionality it is testing.
  
## Expected Outcome

Once you start filling in tests in RentACatTest, you will start to see some of those tests fail for those methods you haven't completed yet for RentACatImpl.  As you start filling in the methods in RentACatImpl, those failures will go away one by one until you again see the output:
```
ALL TESTS PASSED
```
You have come full circle!  But wait, does this mean RentACat is bug-free?  How do you know if your unit tests themselves had defects and that's why they passed, even when RentACat is buggy?We have to actually verify the unit tests themselves to make sure that they are not defective!  One way to verify unit tests is to test them on buggy programs to see if they detect the bugs as they are intended to.  I have created a buggy version of Rent-A-Cat just for this purpose named RentACatBuggy.java.  In order to apply your unit tests to RentACatBuggy, execute the following.  For Windows:
```
runBuggy.bat
```
For Mac or Linux, try doing:
```
bash runBuggy.sh
```

If you run the above, you should get output that looks like [runBuggy.output.txt](runBuggy.output.txt).  Note that I've commented out the following line at TestRunner.java:30 to make the output less verbose:
```
System.out.println(f.getTrace());
```
The above will print a full Java stack trace for every failure.  It is useful when a test fails due to a crash in your program and you want to locate exactly in which source code line the Java exception was thrown.  The defects in RentACatBuggy does not involve crashes due to exceptions so I've temporarily commented it out for brevity.

You can see that all tests fail except the ones for getCat(int id).  That is because I've inserted bugs into RentACatBuggy except for that method.  If your unit test passes any other method, it must be defective.  Time to fix your test.

More products