JUnit. Introduction JUnit is an open source Java testing framework used to write and run repeatable tests JUnit is integrated with several IDEs, including.

Slides:



Advertisements
Similar presentations
Christian Hujer What is AceUnit? How does AceUnit work? How do I use AceUnit? © 2007 Christian Hujer.
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.
Unit Testing. Topics Motivation JUnit framework Basic JUnit tests – static methods Ensure exceptions – instance methods.
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.
JUnit intro Kalvis Apsitis. What are “Programmer Tests”? Programmer Testing is the testing performed by a developer with the goal of verifying the correct.
Ch. 2 Exploring core JUnit. This chapter covers ■ Using the core JUnit classes ■ Understanding JUnit mechanisms ■ Understanding the JUnit lifecycle.
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.
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.
TDD Test-Driven Development. JUnit 4.0 To use annotations need to import org.junit.Test To use assertion need to import org.junit.Assert.* No need to.
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.
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 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.
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:
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAMMING PRACTICES Unit Testing.
Principles of Object Oriented Programming Practical session 2 – part A.
Computer Science and Engineering College of Engineering The Ohio State University JUnit The credit for these slides goes to Professor Paul Sivilotti at.
JUnit in Action SECOND EDITION PETAR TAHCHIEV FELIPE LEME VINCENT MASSOL GARY GREGORY ©2011 by Manning Publications Co. All rights reserved. Slides Prepared.
Unit and Functional Testing Your Flex Applications Mike Nimer Dir. Of Engineering nomee.com.
Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel.
Intoduction to Unit Testing Using JUnit to structure Unit Testing SE-2030 Dr. Rob Hasker 1 Based on material by Dr. Mark L. Hornick.
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.
Test automation / JUnit Building automatically repeatable test suites.
JUnit test and Project 3 simulation. 2 JUnit The testing problems The framework of JUnit A case study Acknowledgement: using some materials from JUNIT.
CSC 395 – Software Engineering Lecture 10: Execution-based Testing –or– We can make it better than it was. Better...faster...agiler.
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 Don Braffitt Updated: 10-Jun-2011.
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.
Enterprise Java v090125Dev Env Overview1 Enterprise Java ( ) Development Environment Overview.
1 Unit Testing with JUnit CS 3331 JUnit website at Kent Beck and Eric Gamma. Test Infected: Programmers Love Writing Tests, Java Report,
JUnit in Action SECOND EDITION PETAR TAHCHIEV FELIPE LEME VINCENT MASSOL GARY GREGORY ©2011 by Manning Publications Co. All rights reserved.
Test a Little, Code a Little Colin Sharples IBM Global Services New Zealand Colin Sharples IBM Global Services New Zealand.
Unit Testing. F-22 Raptor Fighter Manufactured by Lockheed Martin & Boeing How many parts does the F-22 have?
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.
Unit Testing in Eclipse Presented by David Eisler 08/09/2014.
JUnit Automated Software Testing Framework Paul Ammann & Jeff Offutt Thanks in part to Aynur Abdurazik.
CSE 143 Lecture 14: testing.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
Advanced programming Practices
Unit Testing.
Software Construction Lab 10 Unit Testing with JUnit
Don Braffitt Updated: 26-Mar-2013
Unit Testing with JUnit
JUnit Automated Software Testing Framework
Introduction to JUnit CS 4501 / 6501 Software Testing
Unit testing Java programs Using JUnit
This presentation is created for the course COP4331 at UCF
More JUnit CS 4501 / 6501 Software Testing
Junit with.
JUnit Automated Software Testing Framework
JUnit 28-Nov-18.
Credit to Eclipse Documentation
Introduction to JUnit CS 4501 / 6501 Software Testing
More JUnit CS 4501 / 6501 Software Testing
Introduction to JUnit IT323 – Software Engineering II
Section 3 Graphs & Testing
Advanced programming Practices
Section 4: Graphs and Testing
JUnit Reading: various web pages
JUnit SWE 619 Spring 2008.
Junit Tests.
Principles of Object Oriented Programming
Presentation transcript:

JUnit

Introduction JUnit is an open source Java testing framework used to write and run repeatable tests JUnit is integrated with several IDEs, including Eclipse Latest release – JUnit 4.11 (November 14, 2012) Download from and place distribution jar in your classpath or add Maven dependency

JUnit Concepts Test Case – Java class containing test methods Test Method – a no-argument method of a TestCase class annotated Fixture - the initial state of a Test Case Test method contains business logic and assertions – check if actual results equals expected results Test Suite – collection of several Test public void someTestMethod() { … }

Fixture: setUp/tearDown Sometimes tests need to run against the background of a known set of objects (fixture) When you have a common fixture: Add a field for each part of the fixture Annotate a method and initialize the variables in that method Annotate a method to release any permanent resources you allocated during set public void setUp() public void tearDown() {...}

Test public void setUp() {...} gets called once before each test method is public void tearDown() {...} gets called once after each test method has been executed – regardless of whether the test passed or failed (or threw some other exception)

Suite-wide initialization JUnit 4 introduces suite-wide initialization: class-scoped setUp() and tearDown() methods any method will run exactly once before the test methods in that class run any method annotated will run exactly once after all the tests in the class have been run

Assertions Assertions are used to check that actual test results are the same as expected results A set of assert methods is specified in org.junit.Assert (see JUnit JavaDocs) The most often used assertions – assertEquals(), assertNull(), assertSame(), assertTrue() and their opposites – are enough for most situations Assert.fail() is used, if control should not reach that line in the test - this makes the test method to fail

Example import org.junit.Assert; import org.junit.Test; public class public void myTest(){ List customers = customerService.getAllCustomers(); Assert.assertEquals(12, customers.size()); Customer customer = customerService.getById("123"); Assert.assertNotNull("Customer not found", customer); boolean isActive = customerService.isActive(customer); Assert.assertTrue("Customer is not active", isActive); }

Testing Expected Exceptions In JUnit 4, you can write the code that throws the exception and simply use an annotation to declare that the exception is expected: Test fails if exception will not be public void divideByZero() { int n = 2 / 0; }

Ignored Tests Sometimes it’s useful to mark test to be ignored by test runner test that takes an excessively long time to run test that access remote network servers test is failing for reasons beyond your control Such tests can be @Test public void myTest() { }

Timed Tests In JUnit 4 tests can be annotated with a timeout parameter If the test takes longer than the specified number of milliseconds to run, the test public void retrieveAllElementsInDocument() { doc.query("//*"); }

Test Suite Test Cases can be combined into suites import org.junit.*; import org.junit.runner.RunWith; = {CalculatorIntegerTest.class, CalculatorFloatingPointTest.class, CalculatorLogarithmsTest.class }) public class CalculatorTestSuite { // Can leave empty }

Running from Eclipse Right click test class  “Run As”  “JUnit Test” Failed run: Successful run:

Running by Maven > mvn test [INFO] Scanning for projects... [INFO] [INFO] Building web-robot [INFO] task-segment: [test] [INFO] [INFO] [surefire:test] [INFO] Surefire report directory: C:\tools\eclipse\workspace\java-eim-lab01-robot\target\surefire-reports T E S T S Running com.web.robot.BookmarkServiceTest... Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] BUILD SUCCESSFUL [INFO] [INFO] Total time: 9 seconds [INFO] Finished at: Thu Sep 20 09:55:04 EEST 2007 [INFO] Final Memory: 4M/8M [INFO]

References JUnit Home JUnit Cookbook book.htm book.htm An early look at JUnit ibm.com/developerworks/java/library/j- junit4.html ibm.com/developerworks/java/library/j- junit4.html