Unit Testing with JUnit

Slides:



Advertisements
Similar presentations
Java Testing Tools. junit is a testing harness for unit testing. emma is a code coverage tool. The tools can be used in concert to provide statement and.
Advertisements

J-Unit Framework.
MAHDI OMAR JUNIT TUTORIAL. CONTENTS Installation of Junit Eclipse support for Junit Using Junit exercise JUnit options Questions Links and Literature.
T ESTING WITH J UNIT IN E CLIPSE Farzana Rahman. I NTRODUCTION The class that you will want to test is created first so that Eclipse will be able to find.
JUnit Automated Software Testing Framework Paul Ammann & Jeff Offutt Thanks in part to Aynur Abdurazik.
Objectives: Test Options JUnit Testing Framework TestRunners Test Cases and Test Suites Test Fixtures JUnit.
JUnit Automated Software Testing Framework Paul Ammann & Jeff Offutt Thanks in part to Aynur Abdurazik.
1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial.
JUnit, Revisited 17-Apr-17.
Fall 2007CS 2251 Programming Tools Eclipse JUnit Testing make and ant.
JUnit Introduction and Advanced Features. Topics Covered  Junit Introduction  Fixtures  Test Suites  Currency Example.
24-Jun-15 JUnit. 2 Test suites Obviously you have to test your code to get it working in the first place You can do ad hoc testing (running whatever tests.
Writing a Unit test Using JUnit At the top of the file include: import junit.framework.TestCase; The main class of the file must be: public Must extend.
1 CSC/ECE 517 Fall 2010 Lec. 2 Overview of Eclipse Lectures 1.Overview 2.Installing and Running 3.Building and Running Java Classes 4.Debugging 5.Testing.
Presentation Outline What is JUnit? Why Use JUnit? JUnit Features Design of JUnit Downloading JUnit Writing Tests – TestCase – TestSuite Organizing The.
14-Jul-15 JUnit 4. Comparing JUnit 3 to JUnit 4 All the old assertXXX methods are the same Most things are about equally easy JUnit 4 makes it easier.
Unit Testing Discussion C. Unit Test ● public Method is smallest unit of code ● Input/output transformation ● Test if the method does what it claims ●
George Blank University Lecturer. JUnit for Test Driven Development By Vivek Bhagat, George Blank.
Programmer Testing Testing all things Java using JUnit and extensions.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
© Dr. A. Williams, Fall Present Software Quality Assurance – JUnit Lab 1 JUnit A unit test framework for Java –Authors: Erich Gamma, Kent Beck Objective:
Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, , 2013 SWU, Blagoevgrad.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAMMING PRACTICES Unit Testing.
Computer Science and Engineering College of Engineering The Ohio State University JUnit The credit for these slides goes to Professor Paul Sivilotti at.
Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools.
Test automation / JUnit Building automatically repeatable test suites.
Junit At the forefront of Test Driven Development.
JUnit test and Project 3 simulation. 2 JUnit The testing problems The framework of JUnit A case study Acknowledgement: using some materials from JUNIT.
Introduction to JUnit 3.8 SEG 3203 Winter ‘07 Prepared By Samia Niamatullah.
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
JUnit Adam Heath. What is JUnit?  JUnit is a unit testing framework for the Java programming language  It allows developers to swiftly and easily test.
A tool for test-driven development
By Rick Mercer with help from Kent Beck and Scott Ambler Java Review via Test Driven Development (TDD)
JUnit Don Braffitt Updated: 10-Jun-2011.
JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction.
JUnit A framework which provides hooks for easy testing of your Java code, as it's built Note: The examples from these slides can be found in ~kschmidt/public_html/CS265/Labs/Java/Junit.
JUnit. Introduction JUnit is an open source Java testing framework used to write and run repeatable tests JUnit is integrated with several IDEs, including.
JUnit SWE 619 Summer July 18, 2007 SWE 619 (c) Aynur Abdurazik 2 What is JUnit? Open source Java testing framework used to write and run repeatable.
1 Unit Testing with JUnit CS 3331 JUnit website at Kent Beck and Eric Gamma. Test Infected: Programmers Love Writing Tests, Java Report,
JUnit A Unit Testing Framework for Java. The Objective Introduce JUnit as a tool for Unit Testing Provide information on how to: Install it Build a test.
Unit, Regression, and Behavioral Testing Based On: Unit Testing with JUnit and CUnit by Beth Kirby Dec 13, 2002 Jules.
Test a Little, Code a Little Colin Sharples IBM Global Services New Zealand Colin Sharples IBM Global Services New Zealand.
Unit Testing with FlexUnit
Justin Bare and Deric Pang with material from Erin Peach, Nick Carney, Vinod Rathnam, Alex Mariakakis, Krysta Yousoufian, Mike Ernst, Kellen Donohue Section.
Topic: Junit Presenters: Govindaramanujam, Sama & Jansen, Erwin.
JUnit Testing Why we do this and how we can get better.
Unit Testing in Eclipse Presented by David Eisler 08/09/2014.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 42 Testing Using JUnit.
CSE 143 Lecture 14: testing.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
Advanced programming Practices
Software Construction Lab 10 Unit Testing with JUnit
Don Braffitt Updated: 26-Mar-2013
Unit Testing with JUnit
JUnit Automated Software Testing Framework
Computer Science 209 Testing With JUnit.
JUnit Automated Software Testing Framework
Test-driven development (TDD)
Credit to Eclipse Documentation
Introduction to JUnit IT323 – Software Engineering II
Testing Acknowledgement: Original slides by Jory Denny.
Chapter 50 Testing Using JUnit
Section 4: Graphs and Testing
CSE 143 Lecture 5 More ArrayIntList:
JUnit Reading: various web pages
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
JUnit SWE 619 Spring 2008.
JUnit Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from the Eclipse 3.0 and.
Chapter 20: Software Testing - Using JUnit, and Cobertura
JUnit Tutorial Hong Qing Yu Nov 2005.
Presentation transcript:

Unit Testing with JUnit (being revised)

Many Levels of Software Testing Software testing is critical! Testing the specification Unit Testing - test one class Integration Testing - test components and application Acceptance Testing Usability Testing ...

Why Test? 1. Saves time! Testing is faster than fixing "bugs". 2. Testing finds more errors than debugging. 3. Prevent re-introduction of old errors (regression errors). Programmers often recreate an error (that was already fixed) when they modify code. 4. Validate software: does it match the specification?

Psychological Advantage Makes you focus on current tasks. Test-driven development: write tests first ... what the code should do. Then write code that passes the tests Increase satisfaction. Confidence to make changes.

Testing is part of development Agile Development philosophy Test early. Test continually! When To Test? - Test while you are writing the source code - Retest whenever you modify the source code Design Test Code

The Cost of Fixing Defects Discover & fix a defect early is much cheaper (100X) than to fix it after code is integrated. Figure 1.5

An Example A Coin Purse holds coins. It has a capacity that is fixed when the purse is created. capacity is the number of coins (any type) that purse can hold You can insert and withdraw coins within capacity. Purse + Purse( capacity ) + getCapacity( ) : int + getBalance( ) : int + isFull( ) : boolean + insert( Coin ) : boolean + withdraw( amount ) : Coin[*] + count( ) : int insert returns true if coin is inserted.

Writing Test Code from Scratch Purse purse = new Purse(2); // can hold 2 coins // test insert and isFull methods boolean result = purse.insert( new Coin( 10 ) ); if ( ! result ) out.println("ERROR: insert failed"); if ( purse.isFull() ) out.println("ERROR: full"); balance = purse.getBalance( ); if ( balance != 10 ) out.println("ERROR: balance is wrong" ); if ( purse.withdraw(5) != null ) out.println("ERROR: withdraw is wrong"); if ( purse.withdraw(10) == null ) out.println("ERROR: couldn't withdraw 10 Baht");

Too Much Coding! A lot of code for a simple test. Would you write these tests for a real application? No way.

Insight: Factor out Common Code The test code is mostly redundant "boiler plate" code. Automate the redundant code. Create a tool to perform tests and manage output.

JUnit does it! public class StackTest { @Test public void testStackSize( ) { ... } public void testPeek() { public void testPushAndPop() {

Really Simple Tests import org.junit.Assert; public class SimpleTest { import org.junit.Test; import org.junit.Assert; public class SimpleTest { @Test public void testAddition( ) { Assert.assertEquals( 2, 1+1 ); } public void testSqrt() { Assert.assertEquals(5.0, Math.sqrt(25),1.0E-12); public void testPi() { Assert.assertTrue( Math.PI < 3.15 );

Structure of a Test Class Class in Your Project Test Class public class Purse { /** create coin purse */ public Purse(int capacity) { ... } /** insert coins */ public boolean insert( int tens, int fives, int ones){ /** get value of purse */ public int getBalance( ) { public class PurseTest { @Test public void testPurse( ) { // test the constructor } public void testInsert() { // test insert method public void testGetBalance( ) { // test balance method

Example: test the Math class import org.junit.*; public MathTest { @Test // @Test identifies a test method public void testMax( ) { // any public void method name Assert.assertEquals( 7, Math.max(3, 7) ); Assert.assertEquals( 14, Math.max(14, -15) ); } JUnit test methods are in the Assert class. assertEquals(expected, actual ) assertTrue( expression ) assertSame( obja, objb ) expected result actual result

Example: test the Purse constructor import org.junit.*; public PurseTest { /** test the constructor */ @Test public void testPurseConstructor( ) { Purse p = new Purse( 10 ); // capacity 10 Assert.assertEquals("Purse should be empty", 0, p.count() ); Assert.assertEquals("Capacity should be 10", 10, p.getCapacity() ); Assert.assertFalse( p.isFull() ); }

What can you Assert ? JUnit Assert class provides many assert methods Assert.assertTrue( 2*2 == 4 ); Assert.assertFalse( "Stupid Slogan", 1+1 == 3 ); Assert.assertEquals( new Double(2), new Double(2)); Assert.assertNotEquals( 1, 2 ); Assert.assertSame( "Yes", "Yes" ); // same object Assert.assertNotSame("Yes", new String("Yes") ); double[] a = { 1, 2, 3 }; double[] b = Arrays.copyOf( a, 3 ); Assert.assertArrayEquals( a, b ); Assert.assertThat( patternMatcher, actualValue );

Floating Point Tests have a tolerance static final double TOL = 1.0E-8; // tolerance @Test public void testMath( ) { Assert.assertEquals( 1.414213562, Math.sqrt(2), TOL ); } tolerance for floating point comparison Comparison of floating point values should include a tolerance for comparison. Test passes if | expected - actual | <= tolerance

Use import static Assert.* Tests almost always use static Assert methods: @Test public void testInsert( ) { Assert.assertTrue( 1+1 == 2 ); Use "import static" to reduce typing: import static org.junit.Assert.*; public class StupidTest { @Test public void testInsert( ) { assertTrue( 1+1 == 2 );

Test Methods are Overloaded Assert.assertEquals is overloaded (many param. types) assertEquals( expected, actual ); assertEquals( "Error message", expected, actual ); can be any primitive data type or String or Object assertEquals can compare any values. It uses the class's equals( ) method. assertEquals( 10, 2*5 ); assertEquals( "YES", "yes".toUpperCase() ); assertArrayEquals( int[]{1,2,3}, array );

assertEquals and assertSame assertSame tests if two values refer to the same object. Like writing a == b in Java. Object x = "test"; list.add( x ); assertSame( x, list.get(list.size()-1) );

test insertCoin method import org.junit.*; import static org.junit.Assert.* ; public PurseTest { @Test public void insertCoins() { Purse purse = new Purse( 2 ); assertTrue("Couldn't add coin!", p.insertCoin( new Coin( 2 ) ); assertEquals( 2, purse.getBalance( ) ); assertFalse( purse.isFull( ) ); assertTrue("Couldn't add note!", p.insertCoin(new BankNote(50) ); assertEquals( 52, purse.getBalance( ) ); assertTrue( purse.isFull( ) ); Import all static methods from the Assert class.

Running JUnit 4 1. Use Eclipse, Netbeans, or BlueJ (easiest) Eclipse, Netbeans, and BlueJ include JUnit. 2. Run JUnit from command line. CLASSPATH=c:/lib/junit4.1/junit-4.1.jar;. java org.junit.runner.JUnitCore PurseTest 3. Use Ant (automatic build and test tool)

JUnit 4 uses Annotations JUnit 4 uses annotations to identify methods @Test a test method @Before a method to run before each test @After a method to run after each test @BeforeClass method to run one time before testing starts

Before and After methods @Before indicates a method to run before each test @After indicates a method to run after each test public PurseTest { private Purse purse; @Before public void runBeforeTest( ) { purse = new Purse( 10 ); } @After public void runAfterTest( ) { purse = null; } @Test public void testPurse( ) { Assert.assertEquals( 0, purse.count() ); Assert.assertEquals( 10, purse.capacity() ); } Create Test fixture before each test.

@Before (setUp) and @After (tearDown) @Before - method that is run before every test case. setUp( ) is the traditional name. @After - method that is run after every test case. tearDown( ) is the traditional name. setUp( ) setUp( ) another test testFun1( ) one test case testFun2( ) tearDown( ) tearDown( )

Why use @Before and @After ? You want a clean test environment for each test. This is called a test fixture. Use @Before to initialize a test fixture. Use @After to clean up. private File file; // fixture for tests writing a local file @Before public void setUp( ) { file = new File( "/tmp/tempfile" ); } @After public void tearDown( ) { if ( file.exists() ) file.delete();

Testing for an Exception you can indicate that a test should throw an exception. List should throw IndexOutOfBoundsException if you go beyond the end of the list. // this test should throw an Exception @Test( expected=IndexOutOfBoundsException.class ) public void testIndexTooLarge() { List list = new ArrayList(); list.add( "foo" ); list.get( 1 ); // no such element! }

Valid Arguments If an argument is invalid, Coin throws InvalidArgument Exception // this test should throw an Exception @Test( expected=InvalidArgumentException.class ) public void testRejectBadCoins() { Coin coin = new Coin(-1); }

Limit the Execution Time specify a time limit (milliseconds) for a test this test fails if it takes more than 500 milliseconds // this test must finish in less than 500 millisec @Test( timeout=500 ) public void testWithdraw() { // test fixture already created using @Before // method, and inserted coins, too double balance = purse.getBalance(); assertNotNull( purse.withdraw( balance-1 ) ); }

fail! Signal that a test has failed: public void testWithdrawStrategy() { //TODO write this test fail( "Test not implemented yet" ); }

What to Test? Test BEHAVIOR not just methods. May have MANY TESTS for one method. One test may involve several methods.

Designing Tests "borderline" cases: a Purse with capacity 0 or 1 if capacity is 2, can you insert 1, 2, or 3 coins? can you withdraw 0? can you withdraw -1? can you withdraw exactly the amount in the purse? impossible cases: can you withdraw negative amount? can you withdraw balance+1 ? can you withdraw Double.INFINITY ?

Organize Your Test Code Create a separate source tree named "test" for tests avoid mixing application classes and test classes coinpurse/ src/purse/ Purse.java Coin.java test/purse/ PurseTest.java CoinTest.java

Designing Tests typical cases Purse capacity 5. Insert many different coins. When you withdraw, do coins match what you inserted?

Test behavior ... not just methods Example: Purse Test behavior ... not just methods "can I insert same coin twice?" "can I withdraw all the money?" "does withdraw always exactly match what I requested?"

Questions about JUnit 4 Why use: import static org.junit.Assert.*; How do you test if Math.sin(Math.PI/2) is 1 ??? assertEquals( 1.0, Math.sin(Math.PI/2), 1.0E-14 ); How do you test if a String named str is null ??? String str; assertNotNull( str ); or assertTrue( str == null );

Using JUnit in BlueJ 1. From "Tools" menu select "Preferences..." 2. Select "Miscellaneous" tab. 3. Select "Show unit testing tools".

Using JUnit in Eclipse Eclipse includes JUnit 3.8 and 4.x libraries you should use Junit 4 on your projects eclipse will manage running of tests. but, you can write your own test running in the main method Select a source file to test and then...

Using JUnit in Eclipse (2) Select test options and methods to test.

Using JUnit in Eclipse (3) /** Test of the Purse class * @author James Brucker */ public class PurseTest { private Purse purse; private static final int CAPACITY = 10; /** create a new purse before each test */ @Before public void setUp() throws Exception { purse = new Purse( CAPACITY ); } @Test public void testCapacity() { assertEquals("capacity wrong", CAPACITY, purse.capacity()); Write your test cases. Eclipse can't help much with this.

Run JUnit in Eclipse (4) Select the JUnit test case file and choose Run => Run As => JUnit Test Results appear in a new JUnit tab. Click on any result for details and to go to the source code.

References JUnit Home http://www.junit.org JUnit Software & documentation http://www.sf.net/projects/junit Eclipse & Netbeans include Junit, but you still need to install JUnit to get documentation

Quick Starts JUnit 4 in 60 Seconds JUnit Tutorial by Lars Vogel includes how to use JUnit in Eclipse. http://www.vogella.de/articles/JUnit/article.html JUnit 4 in 10 Minutes on JUnit web site

Other Software for Testing TestNG - a better JUnit http://www.testng.org NUnit - Unit testing for .Net Applications http://www.nunit.org

For new code, use the current version of JUnit. JUnit 3.x JUnit 3.x is obsolete But existing software still uses JUnit 3.x, so it is useful to know how to read JUnit 3 tests. For new code, use the current version of JUnit.

Structure of a JUnit 4 Test Class import org.junit.*; // package org.junit import static org.junit.Assert.*; public PurseTest { // don't extend TestCase Purse purse; /**test insert coins */ @Test // use @Test annotation for tests public void testInsertCoins() { // any method name is OK Purse p = new Purse( 1 ); boolean result = p.insertCoin( new Coin( 5 ) ); assertTrue("Couldn't insert coins!", result ); assertFalse( p.insertCoin( new Coin(1) ); // should be full assertEquals( 5.0, p.getBalance( ), 0.001 ); } @Before public void initialize( ) { // any method name is OK purse = new Purse( 10 ); // capacity 10

Structure of a JUnit 3 Test Class import junit.framework.*; // package junit.framework import static junit.framework.Assert.*; public PurseTest extends TestCase { // must extend TestCase Purse purse; // No annotations public void testInsertCoins() { // names must begin with "test" Purse p = new Purse( 1 ); boolean result = p.insertCoin( new Coin( 5 ) ); assertTrue("Couldn't insert coins!", result ); assertFalse( p.insertCoin( new Coin(1) ); assertEquals( 5.0, p.getBalance(), 0.001 ); } // no @Before annotation protected void setUp( ) { // setUp method must use this name purse = new Purse( 10 ); // capacity 10

Key Points in Using JUnit 3.x Test class "extends TestCase" JUnit package is junit.framework import junit.framework.*; Import static methods: import static junit.framework.Assert.*; Must use the naming convention: public void testGetBalance( ) { ... } protected void setUp( ) { ... } protected void tearDown( ) { ... } no annotations (@Before, @After, @Test ...)

JUnit 3 Test Suite For JUnit 3.x your need a method & a constructor: PurseTest( string ) constructor calls super( string ) suite( ) creates a test suite import junit.framework.*; public PurseTest extends TestCase { public PurseTest( String testmethod ) { super( testmethod ); } /** create a test suite automatically */ public static Test suite( ) { TestSuite suite = new TestSuite( PurseTest.class ); return suite; This is standard form of the constructor; just copy it

Compiling and Running Tests You invoke a JUnit TestRunner to run your test suite. JUnit 3.8 provides 3 test runners: junit.textui.TestRunner - console test runner junit.awtui.TestRunner - graphical using AWT junit.swingui.TestRunnger - graphical using Swing > set CLASSPATH = /java/junit3.8.2/junit.jar;. > javac PurseTest.java > java junit.swingui.TestRunner PurseTest Name of your test class as arg.

Another Way to Run Tests Call test runner from your class's main method don't need to invoke junit.*.TestRunner on cmd line public PurseTest extends TestCase { ... public static void main( String [] args ) { junit.swingui.TestRunner.run( PurseTest.class ); } > set CLASSPATH = /java/junit3.8.2/junit.jar;. > javac PurseTest.java > java PurseTest Name of your test class as arg.

Selecting Tests to Run: TestSuite In the example we created a TestSuite using: public static Test suite( ) { TestSuite suite = new TestSuite( PurseTest.class ); return suite; } JUnit uses reflection to locate all methods named "test*". or can specify only the tests you want to run only run these test methods /** create a custom test suite */ public static Test suite( ) { TestSuite suite = new TestSuite( ); suite.addTest( new PurseTest( "testPurse" ) ); // test the constructor suite.addTest( new PurseTest( "testInsertCoins") ); // insert coins return suite; }

JUnit 3 Adaptor for JUnit 4 test class You can run JUnit 3 test cases using JUnit 4 ... import org.junit.Test; import static org.junit.Assert.*; // import adaptor for JUnit 3 import junit.framework.JUnit4TestAdaptor; public PurseTest { // don't extend TestCase /* JUnit 3 calls suite( ) to get a test suite */ public static junit.framework.Test suite( ) { return new JUnit4TestAdaptor( PurseTest.class ); } @Test ... rest of the JUnit 4 tests ...

Questions about JUnit 3 What are the 2 forms of every assert( )? Why use: import static junit.framework.Assert.*; What is the name of ... the test class for "class LineItem" ? your test class extends what other class? the test method for the LineItem constructor? the test method for the getItemID() method?