On this page:
Problem 1: From the tester library to JUnit
11.1 Introduction to JUnit
11.2 Preparing your Project
11.3 A JUnit test
11.3.1 assert  Equals
11.3.2 assert  True and assert  False
11.3.3 Different flavors of assert  Equals
11.4 Timeouts
11.5 Checking for an exception
8.5

Lab 11: An Introduction to the JUnit testing library

Goals: The goal of this lab is to transition from the tester library to the JUnit testing library.

Submission: Submit the Lab 11 assignment in handins by Tuesday 4/11 at 11:59pm. This is an individual submission.

Problem 1: From the tester library to JUnit

Related files:
  IShape.java  

11.1 Introduction to JUnit

All through this semester we have been using the tester library. This is a library written specifically for CS 2510. One of its primary objectives is to help you write meaningful and non-trivial tests while mimicking the general style of test writing in Racket. In this lab we will transition from this custom testing library to a more popular and widely used testing library: JUnit.

JUnit provides a testing framework for Java code. It is widely used in academic and industry settings, and is a powerful library. CS 3500 uses JUnit as well. Unlike existing Java classes that we have been using all semester, JUnit is not part of the JDK. Therefore JUnit must be downloaded separately. More information can be found at the JUnit library website. However most IDEs (including Eclipse) ship with JUnit, so you do not have to download it separately if you are using any of them. There are several similarities between our tester library and JUnit, both in terms of how it works and how to use it.

In this lab we will start with the code from Lecture 4 (linked above), and we will replicate and then add to the tests written in this code using JUnit.

11.2 Preparing your Project
11.3 A JUnit test

The tester library identifies a test method by its name: the name must begin with "test" and should have one argument of type Tester. A JUnit test is a public method inside a public class. Such a method returns void and takes no arguments. Its name does not matter. However one must annotate the method to identify it as a test.

All the instructions below are for the ExamplesShapesInJUnit class.

11.3.1 assertEquals

The JUnit equivalent of t.checkExpect is assertEquals. It takes two arguments: the expected value and the actual value in that order. Note that this order is flipped from checkExpect.

11.3.2 assertTrue and assertFalse

If your expected and actual values are boolean you can use assertTrue and assertFalse instead.

11.3.3 Different flavors of assertEquals

There are several different versions of the assertEquals method.

The test fails because assertEquals uses intensional equality, and therefore two equivalent Circle objects are deemed to be not the same. It uses intensional equality because it uses the equals method for one of the objects and passes the other object to it! Since we did not write an equals method in the Circle class (why would we? We were so naive back then!), it uses the default implementation that compares references.

The assertEquals method can be used to compare two double values as well (equivalent to checkInExact).

Convert other test methods one-by-one to their JUnit equivalents.

11.4 Timeouts

You can add a timeout value to verify that the test takes no more than a certain amount of time. If the test takes more than the allocated time, it will time out and fail. This is useful to check whether tests (and therefore the methods they test) are sufficiently efficient.

To make a test time out if it runs more than 3s, replace @Test above the test to @Test(timeout=3000).

11.5 Checking for an exception

You can write JUnit tests to check whether exceptions are thrown as expected.