Unit Testing with JUnit: A Very Brief Introduction



Similar documents
Java with Eclipse: Setup & Getting Started

Official Android Coding Style Conventions

Hadoop Streaming coreservlets.com and Dima May coreservlets.com and Dima May

For live Java EE training, please see training courses

Apache Pig Joining Data-Sets

Advanced Java Client API

Basic Java Syntax. Slides 2016 Marty Hall,

Building Web Services with Apache Axis2

The Google Web Toolkit (GWT): Declarative Layout with UiBinder Basics

Virtual Machine (VM) For Hadoop Training

The Google Web Toolkit (GWT): The Model-View-Presenter (MVP) Architecture Official MVP Framework

Map Reduce Workflows

Android Programming: Installation, Setup, and Getting Started

HBase Key Design coreservlets.com and Dima May coreservlets.com and Dima May

HBase Java Administrative API

Android Programming Basics

The Google Web Toolkit (GWT): Overview & Getting Started

Hadoop Distributed File System (HDFS) Overview

JHU/EP Server Originals of Slides and Source Code for Examples:

Managed Beans II Advanced Features

Object-Oriented Programming in Java: More Capabilities

Android Programming: 2D Drawing Part 1: Using ondraw

Slides prepared by : Farzana Rahman TESTING WITH JUNIT IN ECLIPSE

Session Tracking Customized Java EE Training:

HDFS Installation and Shell

MapReduce on YARN Job Execution

Debugging Ajax Pages: Firebug

Web Applications. Originals of Slides and Source Code for Examples:

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

2011 Marty Hall An Overview of Servlet & JSP Technology Customized Java EE Training:

HDFS - Java API coreservlets.com and Dima May coreservlets.com and Dima May

What servlets and JSP are all about

Web Applications. For live Java training, please see training courses at

Java Unit testing with JUnit 4.x in Eclipse

Unit Testing & JUnit

Hadoop Introduction coreservlets.com and Dima May coreservlets.com and Dima May

& JSP Technology Originals of Slides and Source Code for Examples:

Application Security

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

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

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

Software Development Interactief Centrum voor gerichte Training en Studie Edisonweg 14c, 1821 BN Alkmaar T:

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

J a v a Quiz (Unit 3, Test 0 Practice)

Unit Testing. and. JUnit

Handling the Client Request: Form Data

Servlet and JSP Filters

Writing Self-testing Java Classes with SelfTest

Java course - IAG0040. Unit testing & Agile Software Development

OUR COURSES 19 November All prices are per person in Swedish Krona. Solid Beans AB Kungsgatan Göteborg Sweden

Testing, Debugging, and Verification

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

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

Localization and Resources

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

Unit Testing JUnit and Clover

The Google Web Toolkit (GWT): JavaScript Native Interface (JSNI)

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

Moving from CS 61A Scheme to CS 61B Java

CS 111 Classes I 1. Software Organization View to this point:

Finding XSS in Real World

Testing Methodology Assignment 1 Unit testing using JUnit

JavaScript: A Crash Course

Mobile Applications Developer Track

Web and Enterprise Applications Developer Track

Rapid Application Development. and Application Generation Tools. Walter Knesel

CmpSci 187: Programming with Data Structures Spring 2015

Beginning POJOs. From Novice to Professional. Brian Sam-Bodden

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

Mobile and Web Applications Developer Track

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

Java (J2SE & J2EE) and Web Development Training Catalog

ID TECH UniMag Android SDK User Manual

VISUAL GUIDE to. RX Scripting. for Roulette Xtreme - System Designer 2.0

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Mobile Applications Developer Track

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Project 4 DB A Simple database program

HybriDroid: Analysis Framework for Android Hybrid Applications

Java/J2EE or Web Developer. Formal Education. Technical knowledge. Spoken Languages

Tutorial on OpenCV for Android Setup

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

ECE 122. Engineering Problem Solving with Java

Software Construction

Preface. Motivation for this Book

An Overview of Servlet & JSP Technology

Pemrograman Dasar. Basic Elements Of Java

Software Development with UML and Java 2 SDJ I2, Spring 2010

1 Introduction. 2 An Interpreter. 2.1 Handling Source Code

Intro to Web Programming. using PHP, HTTP, CSS, and Javascript Layton Smith CSE 4000

CS 1133, LAB 2: FUNCTIONS AND TESTING

Controlling Web Application Behavior

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Integrating Servlets and JSP: The Model View Controller (MVC) Architecture

Overview. What is software testing? What is unit testing? Why/when to test? What makes a good test? What to test?

SSC - Web development Model-View-Controller for Java web application development

Transcription:

coreservlets.com custom onsite training Unit Testing with JUnit: A Very Brief Introduction Originals of slides and source code for examples: http://courses.coreservlets.com/course-materials/java.html Also see Java 8 tutorial: http://www.coreservlets.com/java-8-tutorial/ and many other Java EE tutorials: http://www.coreservlets.com/ Customized Java training courses (onsite or at public venues): http://courses.coreservlets.com/java-training.html Slides 2016 Marty Hall, hall@coreservlets.com For additional materials, please see http://www.coreservlets.com/. The Java tutorial section contains coreservlets.com custom onsite training For customized training related to Java or JavaScript, please email hall@coreservlets.com Marty is also available for consulting and development support Taught by lead author of Core Servlets & JSP, co-author of Core JSF (4 th Ed), and this tutorial. Available at public venues, or custom versions can be held on-site at your organization. Courses developed and taught by Marty Hall JSF 2.3, PrimeFaces, Java programming (using Java 8, for those new to Java), Java 8 (for Java 7 programmers), JavaScript, jquery, Ext JS, Spring Framework, Spring MVC, Java EE 8 MVC, Android, GWT, custom mix of topics Courses available in any state or country. Maryland/DC companies can also choose afternoon/evening courses. Courses Slides developed 2016 Marty and Hall, taught hall@coreservlets.com by coreservlets.com experts (edited by Marty) Hadoop, Hibernate/JPA, HTML5, RESTful Web Services For additional materials, please see http://www.coreservlets.com/. Contact hall@coreservlets.com The Java tutorial section for details contains

Topics in This Section JUnit overview Static imports Modern style assertthat(value, matcher(...)) is, equalto, nullvalue, hasitem, not, anyof, allof, etc. Traditional style assertequals, asserttrue, assertfalse 4 coreservlets.com custom onsite training JUnit Overview Slides 2016 Marty Hall, hall@coreservlets.com For additional materials, please see http://www.coreservlets.com/. The Java tutorial section contains

Motivation Unit testing in general Testing individual methods or small pieces of functionality. Testing overall behavior often not sufficient because not all code cases are used in integrated tests. Whenever you modify code, you can rerun the test cases to verify you are still getting same answer JUnit in particular Most popular and widely used unit testing framework in Java world. Easy to learn basics. Not the only unit testing framework, or even necessarily the best for all situations. Even so, due to its popularity, almost all Java newcomers should start with JUnit first. Not part of official Java SE Integrated with Eclipse and other IDEs 6 7 Using JUnit in Eclipse: Simple Usage (Modern Style) Put @Test above any zero-arg method Eclipse will prompt you to include the JUnit library and will automatically import org.junit.*; Use import static org.junit.assert.*; and import static org.hamcrest.corematchers.*; Lets you use assertthat, etc. without class name Test with assertthat Make tests with assertthat(somevalue, somematcher) R-click in code, Run As JUnit Test Check results printed by Eclipse

Quick Example (Imagine You Wrote Math.min) import org.junit.*; import static org.junit.assert.*; import static org.hamcrest.corematchers.*; public class MinTester { @Test public void testmin() { double d1 = Math.random(); double d2 = Math.random(); assertthat(math.min(d1, d2), is(equalto(math.min(d2, d1)))); 8 Using JUnit in Eclipse: Simple Usage (Traditional Style) Put @Test above any zero-arg method Eclipse will prompt you to include the JUnit library Use import static org.junit.assert.*; Lets you use asserttrue, etc. without class name Test with asserttrue, assertequals, etc. Make tests with asserttrue(value), assertfalse(value), assertequals(val1, val2) R-click in code, Run As JUnit Test Check results printed by Eclipse 9

Home page 10 Documentation http://junit.org/ Many more options than the simple ones shown here Assertions Modern style https://github.com/junit-team/junit/wiki/matchers-and-assertthat Traditional style https://github.com/junit-team/junit/wiki/assertions JavaDoc http://junit.org/javadoc/latest/ For the new style, see especially CoreMatchers http://hamcrest.org/javahamcrest/javadoc/1.3/ coreservlets.com custom onsite training Quick Aside: Static Imports Slides 2016 Marty Hall, hall@coreservlets.com For additional materials, please see http://www.coreservlets.com/. The Java tutorial section contains

Big Idea Motivation Shortens code by letting you refer to static methods without the class name Syntax import static package.class.method; import static package.class.*; Example import static java.lang.math.*;... double d1 = cos(...); double d2 = sin(...); double d3 = random(); // Instead of Math.cos(...) // Instead of Math.sin(...) // Instead of Math.random() 12 coreservlets.com custom onsite training Modern Approach Slides 2016 Marty Hall, hall@coreservlets.com For additional materials, please see http://www.coreservlets.com/. The Java tutorial section contains

14 Overview Setup Make public void zero-arg method marked with @Test Eclipse will offer to add JUnit 4 to the project when you do so Use imports import org.junit.*; import static org.junit.assert.*; import static org.hamcrest.corematchers.*; Create tests with assertthat(val, matcher) int n = somecalculation(); assertthat(n, is(equalto(17)); String s = someothercalculation(); assertthat(s, containsstring("blah")); Run in Eclipse R-click in code, Run As JUnit Test Eclipse will show pass (green) or fail (red) results Core Builtin Matcher: is With simple value, synonymous to equalto assertthat(num, is(12)); assertthat(num, is(equalto(12)); With matcher, just syntactic sugar So omitting is has no effect except for readability assertthat(somestring, is(equalto("blah")); assertthat(somestring, equalto("blah"); assertthat(someobject, is(nullvalue())); assertthat(someobject, nullvalue()); 15

16 Other Core Matcher Types Testing numbers equalto, closeto To use closeto, you must load the full hamcrest library and import static org.hamcrest.number.iscloseto.*; Testing object values equalto, instanceof, nullvalue, notnullvalue, sameinstance Strings and lists containsstring, startswith, endswith, hasitem, hasitems Combining tests not, anyof, allof not takes one matcher anyof and allof take multiple matchers 17 Mini Examples Equality assertthat(foo, is(equalto(bar))) Boolean true assertthat(foo, is(true)) Boolean false assertthat(foo, is(not(true))) Contains subtstring assertthat(string1, containsstring(string2)) Contains elements assertthat(list1, hasitem(blah)) Combined tests assertthat(string1, anyof(nullvalue(), startswith("q"))) assertthat(list1, allof(hasitem("foo"), hasitem("bar"))) Equivalent to: assertthat(list1, hasitems("foo", "bar"))

Testing Example reversestring Should reverse a string, preserving case ispalindrome Should return true if and only if the string reads the same backward and forward, ignoring case differences Examples taken from File IO section 18 Testing Example: Current Implementation public class StringUtils { /** Returns a reversed copy of a non-null String. */ public static String reversestring(string s) { return(new StringBuilder(s).reverse().toString()); /** Checks if a String is a palindrome. Accepts * zero-length or one-length strings, but not null. */ public static boolean ispalindrome(string s) { return(s.equalsignorecase(reversestring(s))); private StringUtils() { 19

JUnit Test (Part 1) package coreservlets; import org.junit.*; import static org.junit.assert.*; import static org.hamcrest.corematchers.*; public class StringUtilsTester { @Test public void testreverse() { Do this before adding the import statements. Eclipse will first say it does not recognize @Test, but when you click on the lightbulb or hit Control-1, Eclipse will offer to add JUnit 4 to the project. Lets you use assertthat without the class name. You must have JUnit 4 in the project before this will be recognized. See JavaDocs for CoreMatchers for details on matchers like is, hasitem, anyof, etc. Of course, you can also add the JAR files manually, if you know how to do so in Eclipse. 20 JUnit Test (Part 2) Because of the @Test annotation, Eclipse knows to run this when you R-click and choose Run As JUnit Test public class StringUtilsTester { @Test public void testreverse() { assertthat("oof", is(equalto(stringutils.reversestring("foo")))); assertthat("rab", is(equalto(stringutils.reversestring("bar")))); assertthat("!zab", is(equalto(stringutils.reversestring("baz!")))); If any of the tests fail, you get red error message in the Eclipse JUnit window. 21 Slightly longer than using assertequals, the traditional approach shown later. But: More readable If you prefer, you can shorten is(equalto(blah)) to is(blah) Type safe: won t compile if argument to equalto is of wrong type

22 JUnit Test (Part 3) @Test public void testpalindromes() { String[] matches = { "a", "aba", "Aba", "abba", "AbBa", "abcdeffedcba", "abcdeffedcba" ; String[] mismatches = { "ax", "axba", "Axba", "abbax", "xabba", "abcdeffedcdax", "axbcdeffedcda" ; for(string s: matches) { assertthat(stringutils.ispalindrome(s), is(true)); for(string s: mismatches) { assertthat(stringutils.ispalindrome(s), is(false)); Slightly longer than asserttrue and assertfalse (traditional approach). But More readable There are often more specific tests such as startswith When combining tests with not, anyof, or allof, the result is much more readable coreservlets.com custom onsite training Traditional Approach Slides 2016 Marty Hall, hall@coreservlets.com For additional materials, please see http://www.coreservlets.com/. The Java tutorial section contains

24 Overview Setup Use imports import org.junit.*; import static org.junit.assert.*; Make zero-arg method marked with @Test Test with asserttrue, assertfalse, assertequals int n = somecalculation(); assertequals(n, 17); String s = someothercalculation(); asserttrue(s.containsstring("blah")); Run in Eclipse R-click in code, Run As JUnit Test Eclipse will show pass (green) or fail (red) results Traditional Approach: Summary assertequals assertequals("some string", somemethodcall( )) assertequals(var1, var2) asserttrue asserttrue(somestring.contains(somesubstring)) asserttrue(somelist.contains(someitem)) asserttrue(someboolean) assertfalse assertfalse(somestring.contains(somesubstring)) assertfalse(somelist.contains(someitem)) assertfalse(someboolean) 25

JUnit Test (Part 1) package coreservlets.java8; import static org.junit.assert.*; import org.junit.*; Lets you use assertequals instead of Assert.assertEquals public class StringUtilsTester { @Test public void testreverse() { assertequals("oof", StringUtils.reverseString("foo")); assertequals("rab", StringUtils.reverseString("bar")); assertequals("!zab", StringUtils.reverseString("Baz!")); 26 If any of the pairs are not equal, you will get error message in the Eclipse JUnit window JUnit Test (Part 2) @Test public void testpalindromes() { String[] matches = { "a", "aba", "Aba", "abba", "AbBa", "abcdeffedcba", "abcdeffedcba" ; String[] mismatches = { "ax", "axba", "Axba", "abbax", "xabba", "abcdeffedcdax", "axbcdeffedcda" ; for(string s: matches) { asserttrue(stringutils.ispalindrome(s)); for(string s: mismatches) { assertfalse(stringutils.ispalindrome(s)); If any of the arguments fail to evaluate to true (asserttrue) or false (assertfalse), you will get error message in the Eclipse JUnit window 27

coreservlets.com custom onsite training My Conventions Slides 2016 Marty Hall, hall@coreservlets.com For additional materials, please see http://www.coreservlets.com/. The Java tutorial section contains Overview There are several possible approaches Modern vs. classic, use is or not, use equalsto inside is or just is, use assertthat or asserttrue when comparing to Booleans, etc. There is general consensus on some Prefer modern to classic Use is; do not use assertequals No consensus on others So I will show my personal style, but no strong reason to follow it if you prefer something else This is not very important! What matters is that you get in the habit of making JUnit tests early, and that you retest when you modify the code 29

Style 1: Using equalsto When comparing calculations, use is(equalto(...)) assertthat(calculation1(), is(equalto(calculation2()))); SomeType val1 = doonething(); SomeType val2 = doanotherthing(); assertthat(val1, is(equalto(val2))); When comparing to literal value, use is(...) without equalto assertthat(calculation1(), is(17)); String val = dosomething(); assertthat(val, is("hello")); 30 Style 2: Boolean Tests For testing a single value, use assertthat and is(true) or is(false) assertthat(myperson.ismarried(), is(true)); boolean isprime = Primes.isPrime(50); assertthat(isprime, is(false)); For multiple tests combined with && and, use asserttrue or assertfalse asserttrue((x > 5) && (x < 50)); assertfalse((n > 100) (Primes.isPrime(n))); 31

coreservlets.com custom onsite training Wrap-Up Slides 2016 Marty Hall, hall@coreservlets.com For additional materials, please see http://www.coreservlets.com/. The Java tutorial section contains 33 Summary Write unit tests from beginning Rerun whenever you change the code Example usage @Test public void somemethod() { String blah = somefancycomputation(); assertthat(blah, allof(notnullvalue(), startswith("q"), not(contains("z"))); List<String> items = somemethod(); assertthat(items, hasitem("foobar")); Blah b1 = docomputationoneway(); Blah b2 = docomputationanotherway(); assertthat(b1, is(equalto(b2)));

coreservlets.com custom onsite training Questions? More info: http://courses.coreservlets.com/course-materials/java.html General Java programming tutorial http://www.coreservlets.com/java-8-tutorial/ Java 8 tutorial http://courses.coreservlets.com/java-training.html Customized Java training courses, at public venues or onsite at your organization http://coreservlets.com/ JSF 2, PrimeFaces, Java 7 or 8, Ajax, jquery, Hadoop, RESTful Web Services, Android, HTML5, Spring, Hibernate, Servlets, JSP, GWT, and other Java EE training Many additional free tutorials at coreservlets.com (JSF, Android, Ajax, Hadoop, and lots more) Slides 2016 Marty Hall, hall@coreservlets.com For additional materials, please see http://www.coreservlets.com/. The Java tutorial section contains