CIS 120: Programming Languages and Techniques I
Spring 2018

Java in Eclipse and JUnit Tests

Making a Java Workspace

For the Java portion of the course, we recommend creating a new Java workspace, so that you don't have to switch settings between OCaml and Java when you move back and forth. To create a new workspace:
  • Click File --> Switch Workspace --> Other ...
  • Click Browse... and navigate to the place where you want to store your Java workspace on disk.
  • Create a new folder for your Java workspace and select it. Give it a meaningful name like "JavaWorkspace" so that you can find it easily.
To switch between OCaml and Java, select File --> Switch Workspace.

Creating a Java Project

  1. Click File --> New --> JavaProject.
  2. Give the project a name and click "Next" not "Finished".
  3. Under the "Libraries" tab, click "Add Library", "JUnit", "Next", select "JUnit 4", and click "Finish".
  4. Import the homework files into the src folder. (Right click on src subfolder of project --> Import --> File System...)

JUnits Tests

Unit testing is a method of testing source code that verifies that individual units are working properly. A unit of code refers to the smallest testable part of an application, in Java this corresponds to a method. JUnit is a Java package that implements unit testing. Eclipse provides tools to automate the creation of JUnit tests. It also provides a good interface for running the tests. Together these two tools make systematically testing your program very easy. In this lab we'll learn how.

Creating JUnit Tests

  1. Right click on your Classname --> New --> JUnit Test Case --> Next.
  2. Select the methods you want to test. Click "Finish".
  3. Fill in tests appropriately. See Assert Types for a list of possible asserts.
  4. To run JUnit Tests, right click on the test class --> Run As --> JUnit Test.