Presentation is loading. Please wait.

Presentation is loading. Please wait.

Testing with JUnit, and ArraySet costs 2014-T2 Lecture 11 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean,

Similar presentations


Presentation on theme: "Testing with JUnit, and ArraySet costs 2014-T2 Lecture 11 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean,"— Presentation transcript:

1 Testing with JUnit, and ArraySet costs 2014-T2 Lecture 11 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Rashina Hoda, and Peter Andreae COMP 103 Marcus Frean

2 RECAP  using collections, implementing ArrayList, comparators, iterators TODAY  A bit of an aside: Testing, using JUnit – used in Assignment #4  analysing costs of the main ArraySet methods. Announcements 2 RECAP-TODAY

3 3 how can we test a program?  Testing a program:  Should test the program as a whole.  Should test all the classes separately  We should probably do both...  Testing a class separately:  Need to create instances of the class and call methods on it  May need set up code to fill the fields of the instance objects.  Should test all the different methods...  Should test methods under different situations...  Should report on the errors it finds.  typically requires writing a test program.

4 4 eg: testing a Collection  Can have a test program with a main method: create an instance of the other class, and call methods on it. public class ArrayListTest { public static void main(String args) { UI.println("Testing ArrayList…"); List list = new ArrayList (); if (! list.isEmpty() ) UI.println(" *** New list is not empty"); if (list.size()!=0 ) UI.println(" *** New list has a size != 0 "); for (int i=0; i<20; i++) { list.add(i, "v"+i); } for (int i=0; i<20; i++) { if ( ! list.get(i).equals("v"+i) ) UI.printf(" *** %d'th value of list should be v%d", i, i); if ( list.size() != 20 ) UI.println(" *** List size should be 20 after adding 20 values"); :

5 5 JUnit documentation

6 6 JUnit makes testing really easy  A special class Assert, with special methods made for testing things: assertTrue("The new list should be empty.", myList.isEmpty()); assertFalse("After adding, the list should not be empty.", myList.isEmpty()); assertEquals("Size should be 1.", myList.size(), 1); assertNull("Item 10 should be null.", myList.get(10));  The 1 st argument is always the message to display if test fails.  Then we can automatically run all the test methods, and report whether they passed or failed.  BlueJ has excellent support for this.

7 7 The magic incantations At the beginning of the tester program we need: import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; import org.junit.After; import org.junit.runner.JUnitCore; and then “@Before” before a “setup” method, e.g. make an empty Set. “@Test” before each test method. We don’t need to use @After, but it “cleans up”. See ArraySetTestCore.java (and...Completion.java) for examples.

8 8 Using JUnit public class ArrayListTest { private List list; @Before public void initialiseEmptyList() { list = new ArrayList (); } @Test public void testIsEmptyOnEmptyList() { assertTrue("A new list should be empty", list.isEmpty()); } @Test public void testAdd() { for (int i = 0; i < 20; i++) { String value = "v" + i; list.add(value); assertEquals("Size should be larger after add", (i+1), list.size()); assertEquals("Item should be in list", value, list.get(list.size()-1)); assertFalse("List should not be empty after add", list.isEmpty()); } }

9 9 BlueJ knows about JUnit tests

10 10 BlueJ displays the test results:

11 11 Costs in ArraySet We’ve looked at ArrayList costs, but not ArraySet. Costs, for ArraySet:  contains:  add:  remove:  All the cost is in the search!  How can we speed up the search?  MONDAY

12 12 Assignment 4 (comes out Monday)  You will have to implement ArraySet  There is a JUnit test (ArraySetTest) to help you test your implementation.  You have to measure how efficient your ArraySet is compared to HashSet, on a Spelling Checker program.  We have given you an implementation of ArrayQueue  It has some errors   You have to write a JUnit test (ArrayQueueTest) to test ArrayQueue (Hint: maybe adapt the one from the ArraySet?) (The tests ought to fail!)


Download ppt "Testing with JUnit, and ArraySet costs 2014-T2 Lecture 11 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean,"

Similar presentations


Ads by Google