In this chapter, the following content will be discussed: Iterative software development, testing with JUnit, Junit ( and ), JUnit for testing programs, framework elements, assert, TestSuite, test of “exceptions”, Junit in eclipse - Setup, from JUnit to . | Software Construction Lab 10 Unit Testing with JUnit SWAT 2010 Iterative Software development + system increment Prioritized functionalities Write acceptance tests Execute acceptance tests Write and execute unit tests “Executed after the development” “Written before” SWAT 2010 Testing with JUnit Junit is a unit test environment for Java programs developed by Erich Gamma and Kent Beck. Writing test cases Executing test cases Pass/fail? (expected result = obtained result?) Consists in a framework providing all the tools for testing. framework: set of classes and conventions to use them. It is integrated into eclipse through a graphical plug-in. Junit ( and ) Test framework test cases are Java code test case = “sequence of operations +inputs + expected values” Test code testDoubleOf2(){ // doubleOf2(); // } Production code int doubleOf2(){ // } JUnit for testing programs JUnit tests “substitute the use of main() to check the program behaviour” All . | Software Construction Lab 10 Unit Testing with JUnit SWAT 2010 Iterative Software development + system increment Prioritized functionalities Write acceptance tests Execute acceptance tests Write and execute unit tests “Executed after the development” “Written before” SWAT 2010 Testing with JUnit Junit is a unit test environment for Java programs developed by Erich Gamma and Kent Beck. Writing test cases Executing test cases Pass/fail? (expected result = obtained result?) Consists in a framework providing all the tools for testing. framework: set of classes and conventions to use them. It is integrated into eclipse through a graphical plug-in. Junit ( and ) Test framework test cases are Java code test case = “sequence of operations +inputs + expected values” Test code testDoubleOf2(){ // doubleOf2(); // } Production code int doubleOf2(){ // } JUnit for testing programs JUnit tests “substitute the use of main() to check the program behaviour” All we need to do is: write a sub-class of TestCase add to it one or more test methods run the test using JUnit .* Framework elements TestCase Base class for classes that contain tests assert*() Method family to check conditions TestSuite Enables grouping several test cases Testsuite Testcase 2 Testcase 1 Testcase 3 An example import ; public class StackTester extends TestCase { public StackTester(String name) { super(name); } public void testStack() { Stack aStack = new Stack(); if(!()) { (“Stack should be empty!”); (10); (-4); (“Last element:“ + ()); (“First element: “ +()); } } Must begin with “test” class Stack { public boolean isEmpty(){ . } public void push(int i){ . } public int pop(){ . } } Assert*() They are public methods defined in the base class TestCase Their names begin with “assert” and are used in .