0% found this document useful (0 votes)
28 views18 pages

3 JUnit

JUnit is a unit testing framework for Java that allows developers to write and run repeatable automated tests to determine whether code is functioning as intended. Key aspects of JUnit include writing test cases and methods that begin with "test", using assertions like assertTrue and assertEquals to validate outcomes, organizing tests into test suites, and utilizing the setUp and tearDown methods. Tests can be run within Eclipse using the JUnit plugin and results are indicated with pass/fail bars. Writing tests first helps increase code quality and makes refactoring and maintenance easier.

Uploaded by

Suneeth Raj
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views18 pages

3 JUnit

JUnit is a unit testing framework for Java that allows developers to write and run repeatable automated tests to determine whether code is functioning as intended. Key aspects of JUnit include writing test cases and methods that begin with "test", using assertions like assertTrue and assertEquals to validate outcomes, organizing tests into test suites, and utilizing the setUp and tearDown methods. Tests can be run within Eclipse using the JUnit plugin and results are indicated with pass/fail bars. Writing tests first helps increase code quality and makes refactoring and maintenance easier.

Uploaded by

Suneeth Raj
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 18

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.

1
Junit (3.x and 4.x)
 Test framework
 test cases are Java code
 test case = “sequence of operations +inputs + expected
values”

 Test code
 Production code
testDoubleOf2(){ //..
int doubleOf2(){ //… doubleOf2();
} //..
}

2
Framework elements

 TestCase
 Base class for classes that contain tests
 assert*()
 Method family to check conditions
 TestSuite
 Enables grouping several test cases

Testcase 1
Testsuite Testcase 2
Testcase 3

3
class Stack {
public boolean isEmpty(){ ... }
An example public void push(int i){ ... }
public int pop(){ ... }

}
 import junit.framework.TestCase;
 public class StackTester extends TestCase {
 public StackTester(String name) {
super(name);
}
 public void testStack() { Must begin with
Stack aStack = new Stack(); “test”
if(!aStack.isEmpty()) {
System.out.println(“Stack should be empty!”);
aStack.push(10);
aStack.push(-4);
System.out.println(“Last element:“ + aStack.pop());
System.out.println(“First element: “ +aStack.pop());
}
 }
4
Method family to check conditions …

Assert*()
 They are public methods defined in the base class
TestCase
 Their names begin with “assert” and are used in test
methods
 es. assertTrue(“stack should be empty”, aStack.empty());
 If the condition is false:
 test fails
 execution skips the rest of the test method
 the message (if any) is printed
 If the condition is true:
 execution continues normally

5
Assert*()
 for a boolean condition
 assertTrue(“message for fail”, condition);
 assertFalse(“message”, condition);
 for object, int, long, and byte values obtained

 assertEquals(expected_value, expression);
 for float and double values
 assertEquals(expected, expression, error);
 for objects references
 assertNull(reference)
 assertNotNull(reference)
 …

6
http://junit.org/apidocs/org/junit/Assert.html
Assert: example

public void testStack() {


Stack aStack = new Stack();
assertTrue(“Stack should be empty!”, aStack.isEmpty());
aStack.push(10);
assertTrue(“Stack should not be empty!”,!aStack.isEmpty());
aStack.push(4);
assertEquals(4, aStack.pop());
assertEquals(10, aStack.pop());
}

class Stack {
public boolean isEmpty(){ ... }
public void push(int i){ ... }
public int pop(){ ... }

}
7
Code Modularization …
One concept at a time …

public class StackTester extends TestCase {


public void testStackEmpty() {
Stack aStack = new Stack();
assertTrue(“Stack should be empty!”, aStack.isEmpty());
aStack.push(10);
assertTrue(“Stack should not be empty!”, !aStack.isEmpty());
}
public void testStackOperations() {
Stack aStack = new Stack();
aStack.push(10);
aStack.push(-4);
assertEquals(-4, aStack.pop());
assertEquals(10, aStack.pop());
}
}

8
TestSuite

junit.framework.*
 Groups several test cases:

public class AllTests extends TestSuite {


public static TestSuite suite() {
TestSuite suite = new TestSuite();
suite.addTestSuite(StackTester.class);
suite.addTestSuite(AnotherTester.class);
return suite;
}
}

9
Test of “Exceptions”

 There are two cases:


1. We expect a normal behavior and then no
exceptions.
2. We expect an anomalous behavior and then an
exception.

10
We expect a normal behavior …

try {
// We call the method with correct parameters
object.method("Parameter");
assertTrue(true); // OK
} catch(PossibleException e){
fail(“method should not fail !!!");
}
class TheClass {
public void method(String p)
throws PossibleException
{ /*... */ }
}
11
We expect an exception …

try {
// we call the method with wrong parameters
object.method(null);
fail(“method should fail!!");
} catch(PossibleException e){
assertTrue(true); // OK
} class TheClass {
public void method(String p)
throws PossibleException
{ /*... */ }
}
12
SetUp() and tearDown()

 setUp() method initialize object(s) under test.


 called before every test method
 tearDown() method release object(s) under test
 called after every test case method.

ShoppingCart cart;
Book book;
protected void setUp() {
cart = new ShoppingCart();
book = new Book(“JUnit", 29.95);
cart.addItem(book);
}

13
Junit in eclipse - Setup

 In Eclipse
 Create a new project
 Open project’s property
window (File ->
Properties)
 Select: Java build path
 Select: libraries
 Add Library
 Select Junit
 Select the type 3.x or 4.x

14
Create a new JUnit test case
Eclipse Menu
File Edit Source Refactor Navigate Search Project Run Window Help

 File
 New
 Junit Test Case
 Set the parameters:
 Junit 3.x or 4.x
 name of the class
 etc.
 Finish

15
Run as JUnit Test
Eclipse Menu
File Edit Source Refactor Navigate Search Project Run Window Help

 Run
 Run As
 Junit Test

16
Red / Green Bar
Fail

Pass

expected <-3> but was <-4>

17
Test First Advantages

• Each method has associated a testcase


• the confidence of our code increases …
• It simplifies:
• refactoring/restructuring
• maintenance
• the introduction of new functionalities
• Test first help to build the documentation
• testcases are good “use samples”
• Programming is more fun …

18

You might also like