Unit testing Java programs Using JUnit

Slides:



Advertisements
Similar presentations
J-Unit Framework.
Advertisements

Unit and Functional Testing with JUnit and Related Tools Greg Barnes University of Washington
Objectives: Test Options JUnit Testing Framework TestRunners Test Cases and Test Suites Test Fixtures JUnit.
Approach of Unit testing with the help of JUnit Satish Mishra
JUnit test distribution Bettina Scharpf ACM 2 University of Applied Science Furtwangen.
Introduction to Eclipse, Unit Testing and JUnit David Rabinowitz.
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.
S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.
JUnit Syed Nabeel. Motivation Unit Testing Responsibility of  developer Rarely done properly Developers Excuse: “I am too much in a hurry”
JUnit, Revisited 17-Apr-17.
20-Jun-15 More About JUnit. Test suites A test suite is a group of JUnit tests You can create a test suite in Eclipse as follows: File  New  Other...
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.
© S. Demeyer, S. Ducasse, O. Nierstrasz Chapter.1 Unit Testing Explained How to support changes? How to support basic but synchronized documentation?
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.
Unit testing C# classes “If it isn’t tested it doesn’t work” Unit testing C# classes1.
Presentation Outline What is JUnit? Why Use JUnit? JUnit Features Design of JUnit Downloading JUnit Writing Tests – TestCase – TestSuite Organizing The.
13-Jul-15 Refactoring II. Books Design Patterns is the classic book by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides Basically a catalog.
George Blank University Lecturer. JUnit for Test Driven Development By Vivek Bhagat, George Blank.
Unit testing Java programs1 Unit testing Java programs Using JUnit 4 “If it isn't tested, it doesn’t work”
Programmer Testing Testing all things Java using JUnit and extensions.
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.
© 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.
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
Principles of Object Oriented Programming Practical session 2 – part A.
Testing in Extreme Programming
1 Testing With The JUnit Framwork Carl-Fredrik Sørensen, PhD Fellow
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.
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.
Refactoring1 Improving the structure of existing code.
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
Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel.
EMBEDDED REAL-TIME, INC. December 8, 2015 Java Unit Mark Mosher Rochester Java Users Group.
JUnit Don Braffitt Updated: 10-Jun-2011.
JUnit. Introduction JUnit is an open source Java testing framework used to write and run repeatable tests JUnit is integrated with several IDEs, including.
S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.
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 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.
Unit testing Java programs1 Unit testing Java programs Using JUnit 4 “If it isn't tested, it doesn’t work”
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.
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.
Topic: Junit Presenters: Govindaramanujam, Sama & Jansen, Erwin.
Test automation / JUnit Building automatically repeatable test suites.
Automated Testing with PHPUnit. How do you know your code works?
Introduction to Unit Testing and JUnit David Rabinowitz.
Getting Started with JUnit Getting Started with JUnit The benefits and ease of writing and running JUnit test cases and test suites. The benefits and ease.
Software Construction Lab 10 Unit Testing with JUnit
Don Braffitt Updated: 26-Mar-2013
Smalltalk Testing - SUnit
Computer Science 209 Testing With JUnit.
Unit testing C# classes
Test-first development
Test-driven development (TDD)
Improving the structure of existing code
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.
Principles of Object Oriented Programming
Presentation transcript:

Unit testing Java programs Using JUnit “If it isn't tested, it doesn’t work” Unit testing Java programs

Requirements for tests Tests must be executable A test must clearly show whether it executed successfully or not The not-so-successfully part of the test must not be buried in a pile of test reports. Unit testing Java programs

Which methods should be tested Test a method if you are not 100% sure that the method is correct. Methods that usually does not need testing Simple get and set methods However, you might call get and set methods in testing other (more complex) methods Simple toString methods Unit testing Java programs

Unit testing Java programs Individual test cases How to write a test case Extends TestCase class MyTest extends TestCase { … } A test method must have the signature public void testXxx() throws ExceptionsAsYouLike Generally you would like one test case pr. Java class The unit to test is a class. Unit testing Java programs

Fixtures: setUp and tearDown Sometimes you have 2 or more tests that must run on the same data. To ease this JUnit introduces the concept of a “fixture”. 2 methods inherited from TestCase meant to be overridden setUp() Executed before each individual test tearDown() Executed after each individual test Unit testing Java programs

Unit testing Java programs Test suites You get a lot of test cases (1 pr. Java class). To several test cases at once you define a “test suite” Make an object of class TestSuite Add the individual test cases to the TestSuite Run the TestSuite Unit testing Java programs

Unit testing Java programs Testing exceptions Testing an expected exception try { method(); fail(“Exception expected”); } catch (ExpectedException ex) { /* ignore */ } A test method must have the signature public void testXxx() throws Exception That means that you can throw any exception from a test method. However, if a test methods throws an exception it means that the test has not passed. Unit testing Java programs

Unit testing Java programs NetBeans assistance NetBeans can assist you in making TestCases for individual Java class and in assembling the test cases into test suites. TestCase Tools → JUnit tests JUnit generates empty (in fact failing) tests for each public / protected method in a Java class. Fill you the empty tests and run the test. Test suites New → Test Suite JUnit generates a test suite of all the test cases in the current test package. Unit testing Java programs

Unit testing Java programs Running tests JUnit test can be run in several ways With a graphical user interface junit.swingui.TestRunner Uses Swing classes for the GUI With a textual user interface Junit.textui.TextRunner NetBeans uses a textual user interface when you run a test case or suite. Unit testing Java programs

The internal structure of JUnit JUnit uses the Reflection API Objects of class Class are submitted to the test suites. From an object Class JUnit finds all methods named testXxx() and executes those methods. Calling setUp() before testXxx() And calling tearDown() after testXxx() Unit testing Java programs

Composite design pattern in JUnit JUnit uses the composite design pattern to organize individual test cases and test suites Unit testing Java programs

Unit testing Java programs Testing in XP Testing is an important discipline in XP (Extreme Programming) XP idea: Create the test before the code to be tested Writing the test makes you thing about detailed design Test is an executable requirements Writing (and running) test will be a positive experience. We know when a class is done When all tests run Unit testing Java programs

Unit testing Java programs References Beck & Gamma JUnit Cookbook, http://junit.sourceforge.net/doc/cookbook/cookbook.htm Kent Beck & Erich Gamma invented JUnit Martin Fowler Refactoring, Addison Wesley 2000 Chapter 4 Building Tests, page 89-102 Extreme Programming, Code the Unit Test First http://www.extremeprogramming.org/rules/testfirst.html Testing is an important discipline in XP (eXtreme Programming), which is another Kent Bech invention. Alex Garrett JUnit antipatterns http://www-128.ibm.com/developerworks/opensource/library/os-junit Unit testing Java programs