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.

Slides:



Advertisements
Similar presentations
Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? © 2007 Christian Hujer.
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.
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.
Multithreading. RHS – SWC 2 What is a thread Inside a single process, multiple threads can be executing concurrently A thread is the execution of (part.
Integration Testing When testing a module in isolation –Called modules need to be replaced –Tested module needs to be called A D ′ E ′ main driver module.
Chapter 3 Implementing Classes. Instance Variables Instance variables store the data of an object; the fields of an object. Instance of a class: an object.
1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial.
JUnit, Revisited 17-Apr-17.
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.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
CS 2511 Fall  Windows:  Start->Specialized Academic Software- >Programming Languages->NetBeans->NetBeans IDE x.y.z  where x.y.z is a version.
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.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
Unit Testing with JUnit Dan Fleck Fall 2007 (For both CS211 and 421… two birds… one lecture! :-)
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:
From BlueJ to NetBeans SWC 2.semester.
Lecture 6 Software Testing and jUnit CS140 Dick Steflik.
Testing. What is Testing? Definition: exercising a program under controlled conditions and verifying the results Purpose is to detect program defects.
Writing Classes (Chapter 4)
CSC 213 – Large Scale Programming. Why Do We Test?
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel.
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
Testing. RHS – SOC 2 Testing So far, we have not really created tests in a systematic manner Testing has been more like ”probing” Making a thorough test.
GIT and JUnit Dr. Andrew Wallace PhD BEng(hons) EurIng
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.
How to Run a Java Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
How to Run a Java Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Exceptions. Exception Abnormal event occurring during program execution Examples –Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
Test automation / JUnit Building automatically repeatable test suites.
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
JUnit test and Project 3 simulation. 2 JUnit The testing problems The framework of JUnit A case study Acknowledgement: using some materials from JUNIT.
1.  Writing snippets of code that try to use methods (functions) from your program.  Each snippet should test one (and only one) function......by calling.
CSC 395 – Software Engineering Lecture 10: Execution-based Testing –or– We can make it better than it was. Better...faster...agiler.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Introduction to JUnit 3.8 SEG 3203 Winter ‘07 Prepared By Samia Niamatullah.
JUnit Dwight Deugo Nesa Matic
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.
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.
Week81 APCS-AB: Java Unit Testing Information today from “Unit Testing in BlueJ” October 28, 2005.
VARIABLES Programmes work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides.
By Rick Mercer with help from Kent Beck and Scott Ambler Java Review via Test Driven Development (TDD)
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Debugging COMP T1.
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,
1 CSC 216 Lecture 3. 2 Unit Testing  The most basic kind of testing is called unit testing  Why is it called “unit” testing?  When should tests be.
Exceptions. Exception  Abnormal event occurring during program execution  Examples Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
Unit, Regression, and Behavioral Testing Based On: Unit Testing with JUnit and CUnit by Beth Kirby Dec 13, 2002 Jules.
PROGRAMMING TESTING B MODULE 2: SOFTWARE SYSTEMS 22 NOVEMBER 2013.
Refactoring Agile Development Project. Lecture roadmap Refactoring Some issues to address when coding.
Unit Testing. F-22 Raptor Fighter Manufactured by Lockheed Martin & Boeing How many parts does the F-22 have?
Unit Testing in Eclipse Presented by David Eisler 08/09/2014.
Module 9: Operator overloading #1 2000/01Scientific Computing in OOCourse code 3C59 Module 9: Operator Overloading In this module we will cover Overloading.
Today protected access modifier Using the debugger in Eclipse JUnit testing TDD Winter 2016CMPE212 - Prof. McLeod1.
Test automation / JUnit Building automatically repeatable test suites.
Unit Testing.
Software Construction Lab 10 Unit Testing with JUnit
An Automated Testing Framework
How to Run a Java Program
JUnit 28-Nov-18.
How to Run a Java Program
Introduction to JUnit IT323 – Software Engineering II
Testing 24-Feb-19.
CS 240 – Advanced Programming Concepts
JUnit Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from the Eclipse 3.0 and.
Presentation transcript:

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 test cases is often very large –How does one test a GUI? –Is functional correctness all that matters?

SWC Testing

SWC Unit testing A Unit Test is aimed at testing a well-defined code module, in Java usually a single class Unit tests are at the functional level –Define test cases in terms of input to class methods (public and private) –Define the expected output for each case –Run the test –Compare expected and actual output

SWC Unit testing NetBeans can create a unit test framework (or ”test harness”) for a project Relies on a Java framework called JUnit (see We also used JUnit in BlueJ

SWC Unit testing in NetBeans Consider our ”classic” BankAccount class, with three methods: –deposit –withdraw –getbalance Having created the class, we can now create a unit test for the class

SWC Unit testing in NetBeans

SWC Unit testing in NetBeans

SWC Unit testing in NetBeans

SWC Unit testing in NetBeans There are quite a lot of options to choose from when generating a test class… For now, just leave them as-is When pressing Finish, a unit test class is generated for us, called BankAccountTest (just choose ”Junit 4.x”) The test class is placed under Test Packages

SWC Unit testing in NetBeans The generated test class does look a bit complex (remember we chose all options) However, we are free to edit it! Remove whatever you do not need NetBeans can only generate a ”skeleton” for the test class – we must complete it

SWC Unit testing in public static void setUpClass() throws Exception { public static void tearDownClass() throws Exception { }

SWC Unit testing in NetBeans The two methods setUpClass and tearDownclass allows us to include any actions needed before and after running all the test class methods, respectively –Dependencies to other classes –Database connection –Etc. Is often not used – then delete it!

SWC Unit testing in public void setUp() { public void tearDown() { }

SWC Unit testing in NetBeans The two methods setUp and tearDown allows us to include any actions needed before and after running each of the test class methods, respectively –Initialising/resetting variable values –Cleaning up data structures –Etc. Is often not used – then delete it!

SWC Unit testing in public void testGetBalance() { System.out.println("getBalance"); BankAccount instance = new BankAccount(); int expResult = 0; int result = instance.getBalance(); assertEquals(expResult, result); // TODO review the generated test code and // remove the default call to fail. fail("The test case is a prototype."); }

SWC Unit testing in NetBeans Notice that a test method does not return a value (true/false) Instead, so-called assertions are used during the test An assertion can succeed or fail A failed assertion throws an exception, and the test case is considered failed

SWC Unit testing in NetBeans Examples of assertions: –assertEquals(expectedValue, ActualValue) –assertTrue(condition) –assertFalse(condition) –assertNotNull(object) –assertNull(object) –assertSame(object, object) –assertNotSame(object, object) –fail() // ALWAYS fails

SWC Unit testing in NetBeans If you inspect the generated test code, you will find that it is not very useful We must – almost always – implement the body of the test methods ourselves We are free to add more test methods than those initially generated – the test framework will run them automatically

SWC Unit testing in NetBeans Once the test methods have been defined properly, we can run the test Choose Run | Test Project, or just press Alt + F6 Result of test is displayed in the output window, with indicative colors

SWC Unit testing in NetBeans

SWC Unit testing in NetBeans

SWC Unit testing considerations In the ideal scenario, all units tests should be completely self-contained Testing of a particular class should not depend on other classes Testing of a particular method should not depend on other methods Isolates cause of failed tests

SWC Unit testing public void testDeposit() { int b = theAcc.getBalance(); theAcc.deposit(500); int a = theAcc.getBalance(); int diff = a – b; assertEquals(diff, 500); }

SWC Unit testing considerations Suppose now that testDeposit fails Which method in BankAccount contains an error…? Is it deposit, or getBalance...? You are wrong! No, you are wrong!

SWC Unit testing public void testCubeVolume() { int volume = theCube.getVolume(); int expVolume = theMathLib.calcCube(theCube.getSide()); assertEquals(volume, expVolume); } Cube getSide getVolume MathLibrary calcCube …

SWC Unit testing considerations Suppose now that testCubeVolume fails Which class contains an error…? Is it Cube or MathLibrary…? You are wrong, again! No, you are wrong again!

SWC Unit testing considerations Testing one functionality often assumes that some other functionality already works correctly… This is quite hard to avoid in practice A rigorous approach is to use so-called test stubs

SWC Unit testing considerations A test stub is a ”simulation” of the behavior of a real class or method (Martin Fowler): Test stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed in for the test

SWC Unit testing considerations Making a test stub –Write the test, calling any external methods that are needed –Substitute all calls to external methods with calls to stub methods (Proxy…?) –Implement the stubs as returning the desired answer without any calculation

SWC Unit testing public void testCubeVolume() { int volume = theCube.getVolume(); int expVolume = theMathLibStub.calcCube(theCube.getSide()); assertEquals(volume, expVolume); }... // Code in MathLibraryStub // Only called with input = 8 in test public int calcCube(int input) { return 512; }

SWC Unit testing considerations Creating a test using stubs consequently can be done – but is quite labor-intensive More pragmatic approach is to use a bottom- up approach –Test basic methods/classes first (methods/classes that do not use other methods/classes) –When basic methods/classes work, test methods/classes that only use basic methods/classes –And so on (dependency tree)

SWC Testing – final remarks We can (almost) never expect to create a completely covering test  Testing is about raising confidence in the correctness of the program Always a compromise between level of confidence and required effort

SWC Testing – final remarks Confidence Effort Student assignment Commer- cial word processor Space Shuttle software

SWC Testing – final remarks Further reading: JUnit test in NetBeans More about Junit in general …and the Net contains a lot of material about test in general!