On this page:
Problem 1: From the tester library to JUnit
12.1 Introduction to JUnit
12.2 Preparing your Project
12.3 A JUnit test
12.3.1 assert  Equals
12.3.2 assert  True and assert  False
12.3.3 Different flavors of assert  Equals
12.4 Timeouts
12.5 Checking for an exception
7.7

Lab 12: 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.

You may work with your current assignment partner in this lab. One submission per team is enough. Please submit what you’ve completed by the end of the lab period on handins.

Problem 1: From the tester library to JUnit

Related files:
  IShape.java  

12.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.

12.2 Preparing your Project
12.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.

12.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.

12.3.2 assertTrue and assertFalse

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

12.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.

12.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).

12.5 Checking for an exception

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