JUnit - Tutorialspoint

Transcription

JUnitAbout the TutorialJUnit is a unit testing framework for Java programming language. JUnit has beenimportant in the development of test-driven development, and is one of a familyof unit testing frameworks collectively known as xUnit, that originated with JUnit.This tutorial explains the use of JUnit in your project unit testing, while workingwith Java. After completing this tutorial you will gain sufficient knowledge in usingJUnit testing framework from where you can take yourself to next levels.AudienceThis tutorial has been prepared for beginners to help them understand the basicfunctionality of JUnit tool.PrerequisitesWe assume you are going to use JUnit to handle all levels of Java projectsdevelopment. So it will be good if you have the knowledge of softwaredevelopment using any programming language, especially Java programming andsoftware testing process.Copyright & Disclaimer Copyright 2015 by Tutorials Point (I) Pvt. Ltd.All the content and graphics published in this e-book are the property of TutorialsPoint (I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy,distribute or republish any contents or a part of contents of this e-book in anymanner without written consent of the publisher.We strive to update the contents of our website and tutorials as timely and asprecisely as possible, however, the contents may contain inaccuracies or errors.Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy,timeliness or completeness of our website or its contents including this tutorial. Ifyou discover any errors on our website or in this tutorial, please notify us atcontact@tutorialspoint.comTable of ContentsAbout the Tutorial . ii

JUnitAudience . iPrerequisites . iCopyright & Disclaimer . iTable of Contents . ii1. OVERVIEW . 1What is JUnit? . 1Features of JUnit . 2What is a Unit Test Case? . 22. ENVIORNMENT SETUP . 3Try it Online Option . 3Local Environment Setup . 33. TEST FRAMEWORK . 8Features of Junit Test Framework . 84. BASIC USAGE. 12Create a Class . 12Create Test Case Class . 12Create Test Runner Class. 135. API . 16Assert Class . 16TestCase Class . 18TestResult Class . 21TestSuite Class . 246. WRITING A TEST . 277. USING ASSERTION . 32Assertion. 32ii

JUnitAnnotation. 358. EXECUTION PROCEDURE . 399. EXECUTING TESTS . 42Create a Class . 42Create Test Case Class . 43Create Test Runner Class . 4310. SUITE TEST . 45Create a Class . 45Create Test Case Classes . 46Create Test Suite Class . 47Create Test Runner Class . 4711. IGNORE A TEST . 49Create a Class . 49Create Test Case Class . 50Create Test Runner Class . 5112. TIME TEST . 54Create a Class . 54Create Test Case Class . 55Create Test Runner Class. 5613. EXCEPTIONS TEST. 57Create a Class . 57Create Test Case Class . 58Create Test Runner Class . 5914. PARAMETERIZED TEST. 60Create a Class . 60iii

JUnitCreate Parameterized Test Case Class . 61Create Test Runner Class . 6215. PLUG WITH ANT . 64Step 1: Download Apache Ant . 64Step 2: Set Ant Environment . 64Step 3: Download JUnit Archive . 65Step 4: Create Project Structure . 65Create ANT Build.xml . 6716. PLUG WITH ECLIPSE . 71Step 1: Download JUnit Archive . 71Step 2: Set Eclipse Environment . 71Step 3: Verify Junit installation in Eclipse . 7217. EXTENSIONS . 77Cactus . 77JWebUnit . 78XMLUnit . 79MockObject . 80iv

1. OVERVIEWJUnitTesting is the process of checking the functionality of an application to ensure it runsas per requirements. Unit testing comes into picture at the developers’ level; it is thetesting of single entity (class or method). Unit testing plays a critical role in helpinga software company deliver quality products to its customers.Unit testing can be done in two ways: manual testing and automated testing.Manual TestingAutomated TestingExecuting a test cases manuallywithout any tool support is knownas manual testing.Taking tool support and executing thetest cases by using an automation toolis known as automation testing.Time-consuming and tedious:Since test cases are executed byhuman resources, it is very slowand tedious.Fast: Automation runs test casessignificantly faster than humanresources.Huge investment in humanresources: As test cases need tobe executed manually, more testersare required in manual testing.Less investment in humanresources: Test cases are executedusing automation tools, so less numberof testers are required in automationtesting.Less reliable: Manual testing isless reliable, as it has to accountfor human errors.More reliable: Automation tests areprecise and reliable.Non-programmable: Noprogramming can be done to writesophisticated tests to fetch hiddeninformation.Programmable: Testers can programsophisticated tests to bring out hiddeninformation.5

JUnitWhat is JUnit?JUnit is a unit testing framework for Java programming language. It plays a crucialrole test-driven development, and is a family of unit testing frameworks collectivelyknown as xUnit.JUnit promotes the idea of "first testing then coding", which emphasizes on settingup the test data for a piece of code that can be tested first and then implemented.This approach is like "test a little, code a little, test a little, code a little." It increasesthe productivity of the programmer and the stability of program code, which in turnreduces the stress on the programmer and the time spent on debugging.Features of JUnit JUnit is an open source framework, which is used for writing and running tests. Provides annotations to identify test methods. Provides assertions for testing expected results. Provides test runners for r

role test-driven development, and is a family of unit testing frameworks collectively known as xUnit. JUnit promotes the idea of "first testing then coding", which emphasizes on setting up the test data for a piece of code that can be tested first and then implemented. This approach is like "test a little, code a little, test a little, code a little." It increases the productivity of the .