Java in Eclipse and JUnit Tests
Making a Java WorkspaceFor 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.
Creating a Java Project
- Click File --> New --> JavaProject.
- Give the project a name and click "Next" not "Finished".
- Under the "Libraries" tab, click "Add Library", "JUnit", "Next", select "JUnit 4", and click "Finish".
- Import the homework files into the src folder. (Right click on src subfolder of project --> Import --> File System...)
JUnits TestsUnit 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
- Right click on your Classname --> New --> JUnit Test Case --> Next.
- Select the methods you want to test. Click "Finish".
- Fill in tests appropriately. See Assert Types for a list of possible asserts.
- To run JUnit Tests, right click on the test class --> Run As --> JUnit Test.