1 JUnit. 2 Unit Testing with JUnit If code has no automated test case written for it to prove that it works, it must be assumed not to work. An API that.

Slides:



Advertisements
Similar presentations
JUnit Tutorial Hong Qing Yu Nov JUnit Tutorial The testing problems The framework of JUnit A case study JUnit tool Practices.
Advertisements

Computer Science 209 Testing With JUnit. Why Test? I don ’ t have time, I ’ ve got a deadline to meet The more pressure I feel, the fewer tests I will.
J-Unit Framework.
An Introduction to JUnit Greg Jackson June Software Quality Assurance & Testing 2 Contact Info Northrop Grumman Corp
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.
Chapter 15: Discovering problems ● There are some tools that we can use to discover problems – Junit: for unit testing – Ant as a make utility – CVS as.
1-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.
Approach of Unit testing with the help of JUnit Satish Mishra
JUnit. What is unit testing? A unit is the smallest testable part of an application. A unit test automatically verifies the correctness of the unit. There.
1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial.
JUnit, Revisited 17-Apr-17.
21-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.
JUnit Introduction and Advanced Features. Topics Covered  Junit Introduction  Fixtures  Test Suites  Currency Example.
22-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.
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.
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.
26-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.
CS 635 Advanced Object-Oriented Design & Programming Spring Semester, 2006 Doc 2 Terms & Testing Jan 24, 2006 Copyright ©, All rights reserved SDSU.
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.
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.
The Design of JUnit Yonglei Tao. Test-First Development  An essential element in eXtreme Programming (XP)  Test is written before the code  As an executable.
Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, , 2013 SWU, Blagoevgrad.
Junit Training Chris Yeung 8 th Sept, Introduction JUnit is a regression testing frameworkJUnit is a regression testing framework Written by Erich.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAMMING PRACTICES Unit Testing.
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
Computer Science and Engineering College of Engineering The Ohio State University JUnit The credit for these slides goes to Professor Paul Sivilotti at.
Testing in Extreme Programming
Unit Testing Bartosz Walter Software Engineering Lecture XXX.
Automated GUI testing How to test an interactive application automatically?
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.
Software Engineering 1 Object-oriented Analysis and Design Chap 21 Test-Driven Development and Refactoring.
CSC 216/001 Lecture 4. Unit Testing  Why is it called “unit” testing?  When should tests be written?  Before the code for a class is written.  After.
JUnit & Eclipse1 DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING CONCORDIA UNIVERSITY Feb 2, 2009 revision 1.2 – Feb 2, 2009 by Emil Vassev & Joey.
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.
JUnit Dwight Deugo Nesa Matic
ESO - Garching 23 June – 02 July, 2003 ACS Course JUnit for Java Unit Testing H. Sommer.
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
JUnit Dwight Deugo Nesa Matic
A tool for test-driven development
Simple Java Unit Testing with JUnit 4 and Netbeans 6.1 Kiki Ahmadi JUG-Bonek.
EMBEDDED REAL-TIME, INC. December 8, 2015 Java Unit Mark Mosher Rochester Java Users Group.
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.
Using UML, Patterns, and Java Object-Oriented Software Engineering Chapter 11, Testing.
S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.
Unit Testing CSSE 514 Programming Methods 4/19/01.
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.
JUnit, Bugzilla James Atlas July 24, 2008 *part of today’s slides courtesy of Dwight Deugo and Nesa Matic under the EPL.
Topic: Junit Presenters: Govindaramanujam, Sama & Jansen, Erwin.
Test automation / JUnit Building automatically repeatable test suites.
Advanced programming Practices
Software Construction Lab 10 Unit Testing with JUnit
Don Braffitt Updated: 26-Mar-2013
Unit testing Java programs Using JUnit
Computer Science 209 Testing With JUnit.
Overview of Eclipse Lectures
JUnit Automated Software Testing Framework
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
Test-driven development (TDD)
Introduction to JUnit IT323 – Software Engineering II
Unit testing with JUnit
Joel Adams and Jeremy Frens Calvin College
JUnit Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from the Eclipse 3.0 and.
JUnit Tutorial Hong Qing Yu Nov 2005.
Presentation transcript:

1 JUnit

2 Unit Testing with JUnit If code has no automated test case written for it to prove that it works, it must be assumed not to work. An API that does not have an automated test case to show how it works must be assumed un- maintainable. Software without automated test cases cannot be economically refactored. Software that that cannot be refactored cannot be extended economically.

3 Don't have the time for unit test? Productivity is directly related to the stability of your code base. – The fewer test cases you write, the less stable your code base becomes. – You spend all your time fixing unintended side effects and bugs.

4 Where to Get JUnit JUnit was written by Erich Gamma and Kent Beck.

5 System.out.println is Not Enough It is hard to tell if a complex system is working because so many System.out.println methods are printing so much garbage. Second, you must determine if something works by looking at a String scrolling by on a console. The string of text scrolling by may make sense the day you wrote it, but will it still make sense in three months? Third, when you make changes, things can break in unexpected ways.

6 Overview of JUnit JUnit is a framework for writing unit tests. A test case defines a fixture to run a related set of tests. Typically, every class that you write should have a test case. A test fixture provides resources: primitive variables and objects that tests need to run. A test suite is a collection of related test cases.

7 Practice 1. Subclass junit.framework.TestCase. 2. If we need fixture objects, override the setUp() method. 3. Define a number of tests that return void and whose method name begins with test, such as testAdd(), testPut(), and testIterator(). 4. If we need to release resources that were part of the fixture, override the tearDown() method. 5. If we need to group a related set of test cases, define a suite of tests.

8 An example package xptoolkit.junit.example; import junit.framework.*; import java.util.Map; import java.util.HashMap; import junit.extensions.*; public class HashMapTest extends TestCase { private Map testMap; private Map testMap2; public HashMapTest(String name) { super(name); } public static Test suite() { return new TestSuite(HashMapTest.class); }

9 public static void main (String[] args) { junit.textui.TestRunner.run (suite()); } private static final String APPLE_KEY = "AppleCEO"; private static final String APPLE_VALUE = "AppleCEO"; protected void setUp() { testMap = new HashMap(); testMap.put(APPLE_KEY, APPLE_VALUE); testMap.put("OracleCEO","Larry Ellison"); testMap2 = new HashMap(); testMap2.put("1", "1"); testMap2.put("2", "2"); }

10 public void testPut(){ String key = "Employee"; String value = "Rick Hightower"; //put the value in testMap.put(key, value); //read the value back out String value2 = (String)testMap.get(key); assertEquals("The value back from the map ", value, value2); } public void testSize(){ assertEquals (2, testMap.size()); } public void testGet(){ assertEquals(APPLE_VALUE, testMap.get(APPLE_KEY)); assertNull(testMap.get("JUNK_KEY")); }

11 public void testPutAll(){ testMap.putAll(testMap2); assertEquals (4, testMap.size()); assertEquals("1", testMap.get("1")); testGet(); } public void testContainsKey(){ assert("It should contain the apple key", testMap.containsKey(APPLE_KEY)); } public void testContainsValue(){ assert(testMap.containsKey(APPLE_VALUE)); }

12 public void testRemove(){ String key = "Employee"; String value = "Rick Hightower"; //put the value in testMap.put(key, value); //remove it testMap.remove(key); //try to read the value back out assertNull(testMap.get(key)); } }

13 Explanation 1 Step 1 is to define a class that derives junit.framework. import junit.framework.*;... public class HashMapTest extends TestCase {

14 Explanation 2 Next, if our test case needs a fixture, we override the setUp() method protected void setUp() { testMap = new HashMap(); testMap.put(APPLE_KEY, APPLE_VALUE); testMap.put("OracleCEO","Larry Ellison"); testMap2 = new HashMap(); testMap2.put("1", "1"); testMap2.put("2", "2"); } the fixture the test case sets up is actually instances of the class under test: the HashMap class. Garbage-collection will destroy the object.

15 Explanation 3 The HashMapTest class defines several tests to test the HashMap class. The JUnit framework uses reflection to look for methods whose names begin with test and uses them as test cases.

16 Explanation 4 It does this when we invoke the TestSuite constructor in the static suite() method, public static Test suite() { return new TestSuite(HashMapTest.class); } the composite design pattern

17 Explanation 5 assertEquals() assertTrue()

18 Explanation 6 Note that the setUp() and tearDown() methods are called before and after every textX() method that is run. Because the setUp() method does not allocate any resources that need to be released, the HashMapTest does not need to override the tearDown() method.

19 JUnit and Ant Copy junit.jar to Ant’s lib directory.

20

21

22 Reading work book/cookbook.htm book/cookbook.htm