Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 103 Testing with JUnit 2016-T2 extras

Similar presentations


Presentation on theme: "COMP 103 Testing with JUnit 2016-T2 extras"— Presentation transcript:

1 COMP 103 Testing with JUnit 2016-T2 extras
Marcus Frean, Rashina Hoda, and Peter Andreae School of Engineering and Computer Science, Victoria University of Wellington 2016-T2 extras

2 How can we test a program?
2 How to test a program? Test the program as a whole? Test all classes separately?  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 methods Should test methods under different situations Should report on the errors it finds  would normally require writing a test program

3 eg: testing a Collection
3 May use a test program with a main method public class ArrayListTest { public static void main(String args) { UI.println("Testing ArrayList…"); List<String> list = new ArrayList<String>(); 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"); :

4 JUnit documentation 4

5 JUnit makes testing easier
5 A special class Assert, with special methods made for testing assertTrue("The new list should be empty.", myList.isEmpty()); assertFalse("After adding, 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 1st argument is the message to display if test fails. test methods can be run automatically BlueJ has dedicated Junit support

6 The necessary ingredients
6 To define a Junit class 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 a “setup” method, e.g. make an empty Set before each test method We don’t need to unless we need to “clean up” See ArraySetTestCore.java (and ...Completion.java) for examples

7 Using JUnit public class ArrayListTest {
7 public class ArrayListTest { private List<String> list; @Before public void initialiseEmptyList() { list = new ArrayList <String>(); } @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); assertFalse("List should not be empty after add", list.isEmpty()); assertEquals("Size should be larger after add", (i+1), list.size()); assertEquals("Item should be in list", value, list.get(list.size()-1));

8 BlueJ knows about JUnit tests
8

9 BlueJ displaying test results
9 test could not be executed test failed


Download ppt "COMP 103 Testing with JUnit 2016-T2 extras"

Similar presentations


Ads by Google