Assignment 2: Writing Unit Tests and Tests as Specifications


Due Friday, February 21

Goals for This Assignment

By the time you have completed this work, you should be able to:

This assignment may be completed in groups.

Step-by-Step Instructions

Step 1: Download Needed Code

Download the following files:

These are the same files which we've been working with in class, and contain a basic linked list implementation.

(Optional) Step 1.5: Port to Another Language

The above files use Java. If you'd prefer to use another language or testing library, you may do so. However, if you choose to use another language/library, you'll need to port the code over to your preferred language/library. Up to a 20% bonus is available for choosing to port to another language; this is an incentive to explore something else, and as a nod to the fact that you're doing extra work to port it.

Step 2: Compile and Run the Code

If you have make installed, you can compile and run this code with the make test command, run in the same directory as all the other files. If you don't have make installed, you can compile your code with:

javac -Xlint:all LinkedList.java NoSuchElementException.java
javac -cp .:hamcrest-2.2.jar:junit-4.13.jar -Xlint:all LinkedListTest.java

...and then test it with:

java -cp .:hamcrest-2.2.jar:junit-4.13.jar org.junit.runner.JUnitCore LinkedListTest

Once you have the tests running, you should see something like:

JUnit version 4.13
...E.E
Time: 0.011
There were 2 failures:
1) testMysteryLength2(LinkedListTest)
arrays first differed at element [0]; expected:<2> but was:<1>
	at org.junit.internal.ComparisonCriteria.arrayEquals(ComparisonCriteria.java:78)
	at org.junit.internal.ComparisonCriteria.arrayEquals(ComparisonCriteria.java:28)
	at org.junit.Assert.internalArrayEquals(Assert.java:534)
	at org.junit.Assert.assertArrayEquals(Assert.java:418)
	at org.junit.Assert.assertArrayEquals(Assert.java:429)
	at LinkedListTest.testMysteryLength2(LinkedListTest.java:30)
	... 32 trimmed
Caused by: java.lang.AssertionError: expected:<2> but was:<1>
	at org.junit.Assert.fail(Assert.java:89)
	at org.junit.Assert.failNotEquals(Assert.java:835)
	at org.junit.Assert.assertEquals(Assert.java:120)
	at org.junit.Assert.assertEquals(Assert.java:146)
	at org.junit.internal.ExactComparisonCriteria.assertElementsEqual(ExactComparisonCriteria.java:8)
	at org.junit.internal.ComparisonCriteria.arrayEquals(ComparisonCriteria.java:76)
	... 38 more
2) testMysteryLength3(LinkedListTest)
arrays first differed at element [0]; expected:<3> but was:<1>
	at org.junit.internal.ComparisonCriteria.arrayEquals(ComparisonCriteria.java:78)
	at org.junit.internal.ComparisonCriteria.arrayEquals(ComparisonCriteria.java:28)
	at org.junit.Assert.internalArrayEquals(Assert.java:534)
	at org.junit.Assert.assertArrayEquals(Assert.java:418)
	at org.junit.Assert.assertArrayEquals(Assert.java:429)
	at LinkedListTest.testMysteryLength3(LinkedListTest.java:40)
	... 32 trimmed
Caused by: java.lang.AssertionError: expected:<3> but was:<1>
	at org.junit.Assert.fail(Assert.java:89)
	at org.junit.Assert.failNotEquals(Assert.java:835)
	at org.junit.Assert.assertEquals(Assert.java:120)
	at org.junit.Assert.assertEquals(Assert.java:146)
	at org.junit.internal.ExactComparisonCriteria.assertElementsEqual(ExactComparisonCriteria.java:8)
	at org.junit.internal.ComparisonCriteria.arrayEquals(ComparisonCriteria.java:76)
	... 38 more

FAILURES!!!
Tests run: 4,  Failures: 2

Step 3: Get Unit Tests Running

Get the tests which are currently failing passing. To do this, you'll need to implement mysteryOperation in LinkedList.java. Before you can implement the operation, you'll need to understand what it does; it should be possible to deduce this by reading the tests in LinkedListTest.java. These tests effectively define a specification of mysteryOperation.

Step 4: Write Tests for Remaining Methods

The remaining methods in LinkedList.java are untested. Add tests to LinkedListTest.java to test these remaining methods. Specifically, you should have tests for:

You should write tests until you're confident that you've covered all the important behaviors of the methods. It's ok if you need to call multiple methods above within the same test; you likely won't be able to test these completely independently of each other.

Step 5: Turn in Your Code Using Canvas

Log into Canvas, and go to the COMP 587 class. Click “Assignments” on the left pane, then click “Assignment 2”. From here, you need to upload the following files:

In addition, if you collaborated with anyone else, be sure to download collaborators.txt and write the names of the people you collaborated with in the file, one per line. Please submit this file along with the other files.

You can turn in the assignment multiple times, but only the last version you submitted will be graded.