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

Slides:



Advertisements
Similar presentations
CMSC 202 Exceptions 2 nd Lecture. Aug 7, Methods may fail for multiple reasons public class BankAccount { private int balance = 0, minDeposit =
Advertisements

Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
Objectives: Test Options JUnit Testing Framework TestRunners Test Cases and Test Suites Test Fixtures JUnit.
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.
Cell phones off Name signs out – congrats Sean! CSE 116 Introduction to Computer Science for Majors II1.
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.
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...
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.
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.
JUnit. Why is testing good? Due to psychological factors, programmers are bad testers. A computer can test much faster than a human Philosophy: “If it.
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.
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.
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.
Unit testing Java programs1 Unit testing Java programs Using JUnit 4 “If it isn't tested, it doesn’t work”
CS5103 Software Engineering Lecture 13 Software Licenses Software Testing.
© 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
EXtreme Programming: Test-First Copyright Rick Mugridge Test-First Programming Rick Mugridge Department of Computer Science University of Auckland.
1 Testing With The JUnit Framwork Carl-Fredrik Sørensen, PhD Fellow
Testing Especially Unit Testing. V-model Wikipedia:
Exceptions1 Syntax, semantics, and pragmatics. Exception create If (some error){ throw new SomeException(”some message”); } Exceptions2.
Sadegh Aliakbary Sharif University of Technology Spring 2012.
Exceptions Syntax, semantics, and pragmatics Exceptions1.
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.
Testing in NetBeans. SWC Testing The ideal test: When the test is passed, the product is ready for delivery! Ideal – but (almost) impossible –Number of.
Distributed Java Programming Distributed Java Programming Class #2 August 22, 2002.
Refactoring1 Improving the structure of existing code.
Introduction to JUnit 3.8 SEG 3203 Winter ‘07 Prepared By Samia Niamatullah.
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.
A tool for test-driven development
Simple Java Unit Testing with JUnit 4 and Netbeans 6.1 Kiki Ahmadi JUG-Bonek.
By Rick Mercer with help from Kent Beck and Scott Ambler Java Review via Test Driven Development (TDD)
Sadegh Aliakbary Sharif University of Technology Spring 2011.
JUnit Don Braffitt Updated: 10-Jun-2011.
Class Builder Tutorial Presented By- Amit Singh & Sylendra Prasad.
JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction.
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.
S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.
1 Unit Testing with JUnit CS 3331 JUnit website at Kent Beck and Eric Gamma. Test Infected: Programmers Love Writing Tests, Java Report,
Agenda for today: The Unified Software Development Process: Test (Note) Test The purpose of testing Artifacts Test in the sw Life Cycle Workers Test procedure.
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.
CS-2852 Data Structures LECTURE 7B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Refactoring1 Improving the structure of existing code.
Unit Testing. F-22 Raptor Fighter Manufactured by Lockheed Martin & Boeing How many parts does the F-22 have?
Software Design– Unit Testing SIMPLE PRIMER ON Junit Junit is a free simple library that is added to Eclipse to all automated unit tests. The first step,
Introduction to Exceptions in Java CS201, SW Development Methods.
Unit Testing.
Software Construction Lab 10 Unit Testing with JUnit
Don Braffitt Updated: 26-Mar-2013
Unit testing Java programs Using JUnit
Unit testing C# classes
Test-first development
Improving the structure of existing code
The purpose of testing Artifacts Test in the sw Life Cycle Workers
Introduction to JUnit IT323 – Software Engineering II
CS 240 – Advanced Programming Concepts
Joel Adams and Jeremy Frens Calvin College
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.
Principles of Object Oriented Programming
Presentation transcript:

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

Testing Larman – a disciplin in UP but very little in practice UPEDU – description Unit testing Java programs2

The Unified Software Development Process: Test procedure (V-model) Test Case 1 Verification of the generation of the billing events through the Customer Care interface StartDelivery Requirement SpecificationAccept Test Program DesignIntegration Test Module Design Module Test Module Coding

Unit testing Java programs4 Testing in XP – Module code Test 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 programs5 Requirements for tests Tests must be executable A test must clearly show whether it executed successfully or not –The not-so-successful part of the test must not be buried in a pile of test reports.

Unit testing Java programs6 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

The Unified Software Development Process: Example testcase Test Case 1 Verification of the generation of the billing events through the Customer Care interface Test Step Input/ActionExpected OutputResult 1Create an account or look up an existing one. Use the apposite link to create a billing event. A billing event is created. The account balance change reflects the presence of the new billing event Account balance: an amount is not shown in a proper way. Se screen shot Test Case 1.

Test cases - Example A property value of int between 2 and 9 Unit testing Java programs8 Test Case Test valueexpectedresult 11 (before boundary)rejected 22 (the boundary)accepted 33 (after boundary)accepted 48 (before boundary)accepted 59 (the boundary)accepted 610 (after boundary)rejected 75 (some value in middle)accepted 8-3 (a negative number)rejected

Do the exercise Unit testing Java programs9

10 Individual test cases How to in JUnit Annotate the test method –No need to extends any classes or use special method names Unlike JUnit 3 Generally you would like one test class pr. Java class –The unit to test is a class.

Unit testing Java programs11 Fixtures: Before and After Sometimes you have 2 or more tests that must run on the same data. –To ease this JUnit introduces the concept of a “fixture”. You can annotate 2 methods in your test class from Executed before each individual test Used to initialize test data Used quite often Executed after each individual test Used to clean up after the test –Examples: Close database or socket connections Not used very often

Unit testing Java programs12 How to test exceptions JUnit 3: Testing an expected exception try { method(); fail(“Exception expected”); } catch (ExpectedException ex) { /* ignore */ } JUnit 4: Testing an expected exception –Use an (expected = SomeException.class) public void testMetod() { … } If testMethod() does not throw SomeException the test fails.

Unit testing Java programs13 NetBeans assistance JUnit is a plugin to NetBeans –And many other IDE’s NetBeans can assist you in making TestCases for individual Java class and in assembling the test cases into test suites. –Right click the class you want to test –Tools → JUnit tests Choose JUnit 4 (not JUnit 3) –JUnit generates empty tests for each public / protected method in a Java class. –Fill you the empty tests and run the test. Like you run an ordinary program

Unit testing Java programs14 References Beck & Gamma JUnit Cookbook, Kent Beck & Erich Gamma invented JUnit Martin Fowler Refactoring, Addison Wesley 2000 Chapter 4 Building Tests, page Extreme Programming, Code the Unit Test First Testing is an important discipline in XP (eXtreme Programming), which is another Kent Bech invention. Alex Garrett JUnit antipatterns