JUnit Dwight Deugo dwightespirity com Nesa Matic nesaespirity

  • Slides: 24
Download presentation
JUnit Dwight Deugo (dwight@espirity. com) Nesa Matic (nesa@espirity. com)

JUnit Dwight Deugo (dwight@espirity. com) Nesa Matic (nesa@espirity. com)

Additional Contributors n None as of September, 2005 2 © 2003 -2005, Espirity Inc.

Additional Contributors n None as of September, 2005 2 © 2003 -2005, Espirity Inc.

Module Road Map 1. JUnit 3 © 2003 -2005, Espirity Inc.

Module Road Map 1. JUnit 3 © 2003 -2005, Espirity Inc.

Module Road Map 1. JUnit § § § What is JUnit? Where Does it

Module Road Map 1. JUnit § § § What is JUnit? Where Does it Come From? Working with Test. Cases Working with Test. Suites JUnit Window 4 © 2003 -2005, Espirity Inc.

What is JUnit? n n n Regression testing framework Written by Erich Gamma and

What is JUnit? n n n Regression testing framework Written by Erich Gamma and Kent Beck Used for unit testing in Java Open Source Released under IBM's CPL 5 © 2003 -2005, Espirity Inc.

Where Does JUnit Come From? n n JUnit’s web site: http: //junit. org/index. htm

Where Does JUnit Come From? n n JUnit’s web site: http: //junit. org/index. htm Eclipse includes JUnit q n Eclipse provides new GUI to run JUnit test cases and suites You can run your unit tests outside of Eclipse If you wish using Test. Runner q Using JUnit’s Window q 6 © 2003 -2005, Espirity Inc.

Eclipse JUnit Setup n n n Eclipse preferences can be set in the JUnit

Eclipse JUnit Setup n n n Eclipse preferences can be set in the JUnit Preferences window For the most part you can leave these alone Filters needed to identify packages, classes, or patterns that should not be shown in the stack trace of a test failure 7 © 2003 -2005, Espirity Inc.

JUnit Test Cases n Test case q n n Runs multiple tests Implemented a

JUnit Test Cases n Test case q n n Runs multiple tests Implemented a subclass of Test. Case Define instance variables that store the state of the tests in the class Initialize Test. Case by overriding set. Up method Clean-up after test case is done by overriding tear. Down method 8 © 2003 -2005, Espirity Inc.

Creating Test. Cases in Eclipse… n n n Create a new package to contain

Creating Test. Cases in Eclipse… n n n Create a new package to contain your test case classes Add the JUnit JAR file to the project’s buildpath Or, will be done for you the first time you build a test case 9 © 2003 -2005, Espirity Inc.

…Creating Test. Cases in Eclipse n n Select your testing package From the context

…Creating Test. Cases in Eclipse n n Select your testing package From the context menu select New JUnit Test Case In the next Window fill in the name of your test case This will create the corresponding class in your testing package 10 © 2003 -2005, Espirity Inc.

Test. Case Template package com. espirity. course. testing; import junit. framework. Test. Case; public

Test. Case Template package com. espirity. course. testing; import junit. framework. Test. Case; public class First. Test. Case extends Test. Case { public First. Test. Case(String arg 0) { super(arg 0); } public static void main(String[] args) { } protected void set. Up() throws Exception { super. set. Up(); } protected void tear. Down() throws Exception { super. tear. Down(); } } 11 © 2003 -2005, Espirity Inc.

Adding Tests to Test. Cases n Any method in a Test. Case class is

Adding Tests to Test. Cases n Any method in a Test. Case class is considered a test if it begins with the word test q n You can write many tests (have many test methods) Each test method should use a variety of assert methods to test things about the state of their classes under tests q Assert methods are inherited 12 © 2003 -2005, Espirity Inc.

Assert Methods n Assert methods include: assert. Equal(x, y) q assert. False(boolean) q assert.

Assert Methods n Assert methods include: assert. Equal(x, y) q assert. False(boolean) q assert. True(boolean) q assert. Null(object) q assert. Not. Null(object) q asset. Same(first. Object, second. Object) q assert. Not. Same(first. Object, second. Object) q 13 © 2003 -2005, Espirity Inc.

Adding Two Tests to Test. Case package testing; import junit. framework. Test. Case; public

Adding Two Tests to Test. Case package testing; import junit. framework. Test. Case; public class First. Test. Case extends Test. Case { public First. Test. Case(String arg 0) { super(arg 0); } public static void main(String[] args) {} protected void set. Up() throws Exception { super. set. Up(); } protected void tear. Down() throws Exception { super. tear. Down(); } public void test. Compare. Succeed() { assert. Equals(0, 0); //this assertion will succeed } public void test. Compare. Fail() { assert. Equals(0, 1); //this assertion will fail } } 14 © 2003 -2005, Espirity Inc.

Running Test. Case n n Select Test. Case class From the Run menu select

Running Test. Case n n Select Test. Case class From the Run menu select Run As JUnit Test n n This will run the tests in your Test. Case class along with the setup and teardown methods You will then get a report in the JUnit Window 15 © 2003 -2005, Espirity Inc.

JUnit Window… n n Red indicates a test has failed If you wish to

JUnit Window… n n Red indicates a test has failed If you wish to see the tests in Test. Case click on the Hierarchy tab You can see which test failed You can see the call trace leading to the failure 16 © 2003 -2005, Espirity Inc.

…JUnit Window n n You can see how many tests ran Errors occur when

…JUnit Window n n You can see how many tests ran Errors occur when exceptions are thrown How many failures occurred You can see the details of the failure 17 © 2003 -2005, Espirity Inc.

Creating JUnit Test. Suite… n n n Test Suite q Runs multiple test cases

Creating JUnit Test. Suite… n n n Test Suite q Runs multiple test cases or suites Implemented as subclass of Test. Suite To create a Test. Suite q Select your testing package q From the context menu select New Other… Java JUnit q Then from the Wizard select JUnit Test Suite 18 © 2003 -2005, Espirity Inc.

…Creating JUnit Test. Suite n n Fill in the name of your Test. Suite

…Creating JUnit Test. Suite n n Fill in the name of your Test. Suite Class Select the Test. Cases to include in your Test. Suite 19 © 2003 -2005, Espirity Inc.

Test. Suite Template package com. espirity. course. testing; import junit. framework. Test; public class

Test. Suite Template package com. espirity. course. testing; import junit. framework. Test; public class All. Inclusive. Test. Suite { public static Test suite() { Test. Suite suite = new Test. Suite("Test for com. espirity. course. testing"); //$JUnit-BEGIN$ suite. add. Test. Suite(First. Test. Case. class)); suite. add. Test. Suite(Second. Test. Case. class)); //$JUnit-END$ return suite; } } 20 © 2003 -2005, Espirity Inc.

Running Test. Suite n n Select Test. Suite class From the Run menu select

Running Test. Suite n n Select Test. Suite class From the Run menu select Run As JUnit Test This will run the test cases in your Test. Suite class You will then get a report in the JUnit Window 21 © 2003 -2005, Espirity Inc.

Interesting Point n n The JUnit classes Test. Case and Test. Suite both implement

Interesting Point n n The JUnit classes Test. Case and Test. Suite both implement the JUnit Test interface Therefore, you can add JUnit Test. Suites to other Test. Suites public static Test suite() { Test. Suite suite = new Test. Suite("Test for testing"); //$JUnit-BEGIN$ suite. add. Test. Suite(First. Test. Case. class); suite. add. Test. Suite(Second. Test. Case. class); suite. add. Test(All. Tests. suite()); //$JUnit-END$ return suite; } } 22 © 2003 -2005, Espirity Inc.

Summary n You have learned: What is JUnit q How it is integrated into

Summary n You have learned: What is JUnit q How it is integrated into Eclipse q How to write Test Cases q How to write Test Suites q How do use JUnit in Eclipse q 23 © 2003 -2005, Espirity Inc.

Labs! Lab: Using JUnit 24 © 2003 -2005, Espirity Inc.

Labs! Lab: Using JUnit 24 © 2003 -2005, Espirity Inc.