Java Unit testing with JUnit 4.x in Eclipse



Similar documents
Unit Testing & JUnit

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

+ Introduction to JUnit. IT323 Software Engineering II By: Mashael Al-Duwais

Unit Testing and JUnit

Unit Testing JUnit and Clover

Slides prepared by : Farzana Rahman TESTING WITH JUNIT IN ECLIPSE

Table of Contents. LESSON: The JUnit Test Tool...1. Subjects...2. Testing What JUnit Provides...4. JUnit Concepts...5

Licensed for viewing only. Printing is prohibited. For hard copies, please purchase from

Unit Testing. and. JUnit

JUnit Automated Software Testing Framework. Jeff Offutt. SWE 437 George Mason University Thanks in part to Aynur Abdurazik. What is JUnit?

JUnit. Introduction to Unit Testing in Java

Testing, Debugging, and Verification

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Testing Methodology Assignment 1 Unit testing using JUnit

Writing Self-testing Java Classes with SelfTest

Effective unit testing with JUnit

A Practical Guide to Test Case Types in Java

Appium mobile test automation

LAB 6: Code Generation with Visual Paradigm for UML and JDBC Integration

Using JUnit in SAP NetWeaver Developer Studio

RUP. Development Process. Iterative Process (spiral) Waterfall Development Process. Agile Development Process. Well-known development processes

Eclipse installation, configuration and operation

SpiraTest / SpiraTeam Automated Unit Testing Integration & User Guide Inflectra Corporation

Test-Driven Development

Crystal Reports Integration Plugin for JIRA

Software Engineering. Top-Down Design. Bottom-Up Design. Software Process. Top-Down vs. Bottom-Up 13/02/2012

Tools for Integration Testing

Development Environment and Tools for Java. Brian Hughes IBM

JustClust User Manual

Getting Started with the Internet Communications Engine

CI/CD Cheatsheet. Lars Fabian Tuchel Date: 18.March 2014 DOC:

Author: Sascha Wolski Sebastian Hennebrueder Tutorials for Struts, EJB, xdoclet and eclipse.

Vim, Emacs, and JUnit Testing. Audience: Students in CS 331 Written by: Kathleen Lockhart, CS Tutor

Understanding class paths in Java EE projects with Rational Application Developer Version 8.0

JUnit - A Whole Lot of Testing Going On

With a single download, the ADT Bundle includes everything you need to begin developing apps:

Java Bluetooth stack Acceptance Test Plan Version 1.0

TESTING WITH JUNIT. Lab 3 : Testing

Jemmy tutorial. Introduction to Jemmy testing framework. Pawel Prokop. March 14,

Service Integration course. Cassandra

IOIO for Android Beginners Guide Introduction

ID TECH UniMag Android SDK User Manual

How To Test In Bluej

Tutorial for Creating Resources in Java - Client

Selenium Automation set up with TestNG and Eclipse- A Beginners Guide

INTERCALL ONLINE Administrator Invoices User Guide

tools that make every developer a quality expert

UI Performance Monitoring

Overview of Web Services API

Performance Testing from User Perspective through Front End Software Testing Conference, 2013

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

Hands on exercise for

Generating Automated Test Scripts for AltioLive using QF Test

Mapping the ITS File Server Folders to Mosaic Windows

The goal with this tutorial is to show how to implement and use the Selenium testing framework.

Using Internet or Windows Explorer to Upload Your Site

Getting Started using the SQuirreL SQL Client

Official Android Coding Style Conventions

Advertiser Campaign SDK Your How-to Guide

How To Print On A Computer On A Macbook With A Powerpoint 3D (For Free) On A Pc Or Macbook 2 (For Cheap) On Pc Or Pc Or Ipad (Forfree) On An Ipad 3D Or

bbc Developing Service Providers Adobe Flash Media Rights Management Server November 2008 Version 1.5

TOOL EVALUATION REPORT: FORTIFY

Installation Guide of the Change Management API Reference Implementation

LAB 1. Familiarization of Rational Rose Environment And UML for small Java Application Development

Arduino & Android. A How to on interfacing these two devices. Bryant Tram

IBI Group FTP: Usage Instructions

GridWorld Installation Guide

Midland BT Updater BTUpdater Program Program file (x86) ), Midland

Implementing a SAS 9.3 Enterprise BI Server Deployment TS-811. in Microsoft Windows Operating Environments

PTC Integrity Eclipse and IBM Rational Development Platform Guide

Installing (1.8.7) 9/2/ Installing jgrasp

Hypercosm. Studio.

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Developer Guide. Android Printing Framework. ISB Vietnam Co., Ltd. (IVC) Page i

Remote Viewer Recording Backup

JobScheduler and Script Languages

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

How to Install Eclipse. Windows

Creating a 2D Game Engine for Android OS. Introduction

EDGETECH FTP SITE CUSTOMER & VENDOR ACCESS

Capabilities of a Java Test Execution Framework by Erick Griffin

Supplement I.B: Installing and Configuring JDK 1.6

Appendix A Using the Java Compiler

Download and Installation Instructions. Android SDK and Android Development Tools (ADT) Microsoft Windows

PROCEDURE TO JOIN WEBEX MEETING FOR REMOTE SUPPORT

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

Unit-testing with JML

ECE 122. Engineering Problem Solving with Java

IBM Tivoli Workload Scheduler Integration Workbench V8.6.: How to customize your automation environment by creating a custom Job Type plug-in

Tutorial 5: Developing Java applications

Application Note: Failover with Double- Take Availability and the Scale HC3 Cluster. Version 2.0

CS108, Stanford Handout #33 Young. HW5 Web

Web Services using Tomcat and Eclipse

L01: Using the WebSphere Application Server Liberty Profile for lightweight, rapid development. Lab Exercise

Transcription:

Lars Vogel <webmaster@vogella.de> Version 0.2 Copyright 2007 Lars Vogel 30.06.2007 Abstract This article gives a short overview of JUnit 4.x and its usage within Eclipse. You should be able to run tests with JUnit from Eclipse after reading this chapter. Table of Contents 1. General... 1 1.1. Unit Testing... 1 1.2. Installation... 2 2. Introduction in Writing a Test... 2 2.1. Overview... 2 2.2. Write your first test... 2 2.3. Run your first test (Eclipse)... 3 2.4. Run your first test (command line)... 3 3. JUnit with Eclipse... 3 3.1. Preparation... 3 3.2. Create a normal class... 3 3.3. Create a test class... 4 3.4. Create a test suite... 4 4. JUnit (more) in Detail... 5 4.1. Annotations... 5 4.2. Test statements... 5 5. Links and Literature... 6 1. General 1.1. Unit Testing A unit test is a piece of code written by a developer that exercises a very small, specific area of functionality of the code being tested. It can be used to ensure that existing functionality is still working after changes to the coding or the environment the program is running in. JUnit 4.x is a framework for automatically testing java code originally written by Erich Gamma and Kent Beck. It uses annotation to identify what are the test methods. A very important assumption of JUnit is that the all test can be performed in an arbitrary order hence each test should stand alone and not depend on another test. 1

1.2. Installation Download JUnit4.x.jar from the JUnit website here [6] and add it to classpath. Tip You can use Junit independently of Eclipse but using Junit with Eclipse is easier due to the very good integrated JUnit support. 2. Introduction in Writing a Test 2.1. Overview JUnit uses annotations to identify which methods should be used for testing. To write a test Annotate a method with @org.junit.test When you want to check two value for equality, import org.junit.assert.* statically1, call assertequals() and pass both values. Static imports are available since Java 5.0: static import, e.g. import static org.junit.assert.*; 2.2. Write your first test Write the following java source file: package testingtest; import static org.junit.assert.assertequals; import org.junit.test; public class MyFirstJUnitTest { @Test public void simpleadd() { int result = 1; int expected = 1; assertequals(result, expected); 2

2.3. Run your first test (Eclipse) If your are using Eclipse select your new file, right mouse click on it and select Run As -> JUnit test Eclipse will display the result via a green or red bar. 2.4. Run your first test (command line) I the JUnit distribution you have a testrunner which can also run your JUnit tests. Switch to the command line and type the following command. org.junit.junitcore.runclasses(testclass1.class,...); 3. JUnit with Eclipse 3.1. Preparation Create a new project. Add a folder lib to this project. Copy your Junit jar into this folder and add it to the buld path of Eclipse. See here [http://www.vogella.de/articles/eclipse/article.html] on how to do this. Create a new source folder junit for your project. To do so right mouse click on your project, select properties and choose the java build path. Select the tab source code. Press "Add folder" then then press "Create new folder". Create the folder junit. 3.2. Create a normal class Create his extremely boring class "MyClass". We will use this class for testing. public class MyClass { public int multiply(int x, int y) { return x / y; 3

3.3. Create a test class Add JUnit.jar to your build path Select your new class, right mouse click and select New ->JUnit Test case, change the source folder to JUnit Press next and select the methods which you want to test. Create a test with the following code. import static org.junit.assert.assertequals; import org.junit.test; public class MyClassTest { @Test public void testmultiply() { MyClass tester = new MyClass(); assertequals("result", 50, tester.multiply(10, 5)); Right click on your new test class and select Run-As-> Junit Test. The result should be the following. This is due to the fact that our multiplier class is currently doing division. Fix the bug and re-run test to get a green light. 3.4. Create a test suite If you have several tests you can combine them into a test suite. 4

Select your test classes, right mouse click-> New-> Other -> JUnit -Test Suite Select next and select the methods you would like to have test created for. Press finish. 4. JUnit (more) in Detail 4.1. Annotations The following give an overview of the available annotations in JUnit 4.x Table 1. Annotations Annotation @Test public void method() @Before public void method() @After public void method() @BeforeClass public void method() @AfterClass public void method() @Ignore Description Annotation @Test identifies that this method is a test method. Will perform the method() before each test. This method can prepare the test environment, e.g. read input data, initialize the class) Test method must start with test Will perform the method before the start of all tests. This can be used to perform time intensive activities for example be used to connect to a database Will perform the method after all tests have finished.. This can be used to perform clean-up activities for example be used to disconnect to a database Will ignore the test method, e.g. useful if the underlying code has been changed and the test has not yet been adapted. @Test(expected=IllegalArgumentException.class) Tests if the method throws the named exception @Test(timeout=100) Fails if the method takes longer then 100 milliseconds 4.2. Test statements The following gives an overview of the available test methods: 5

Table 2. Test methods Statement fail(string) asserttrue(true); assertsequals([string message], expected, actual) assertsequals([string message], expected, actual, tolerance) assertnull([message], object) assertnotnull([message], object) assertsame([string], expected, actual) assertnotsame([string], expected, actual) asserttrue([message], boolean condition) try {a.shouldthroughexception(); fail( Failed ) catch (RuntimeException e) {asserttrue(true); Description Let the method fail, might be usable to check that a certain part of the code is not reached. True Test if the values are the same. Note: for arrays the reference is checked not the content of the arrays Usage for float and double; the tolerance are the number of decimals which must be the same Checks if the object is null Check if the object is not null Check if both variables refer to the same object Check that both variables refer not to the same object Check if the boolean condition is true. Alternative way for checking for exceptions 5. Links and Literature http://www.junit.org/ - Junit Homepage [http://www.junit.org/]. http://junit.sourceforge.net/doc/cookbook/cookbook.htm - The JUnit Cookbook from Erich Gamma and Kent Beck [http://junit.sourceforge.net/doc/cookbook/cookbook.htm]. 6