Hands on Testing Java

Running Java Programs Reference

What are you trying to execute?

Re-running Something

Re-run whatever was run last with Run -> Run Last Launched. Or, use the Run button in the toolbar.

Running an Application in Eclipse

To set up a runtime configuration for a Java application (or driver)

  1. Select the driver class in the Package Explorer.
  2. Right-click on the driver class, and select the Run -> Run As -> Java Application.

Running an Application in Eclipse which Needs Command-Line Arguments

To set up a runtime configuration for a Java application which needs command-line arguments

  1. Select your project (e.g., Hands on Testing Java) in the Package Explorer.
  2. Select Run -> Run....
  3. Select Java -> Application in the list on the left.
  4. Press the New button.
  5. Enter in a new Name (probably the name of the driver class).
  6. Use the Search... button to the right of the the Main class field to find the driver class.
  7. Click on the Arguments tab.
  8. Enter the command-line arguments in the Program arguments text area.
  9. Hit the Run button.

Return to the Arguments tab to change the command-line arguments; alternatively, create multiple runtime configurations with the same main class but different command-line arguments.

Running JUnit Test Cases in Eclipse

To set up a runtime configuration for a JUnit test case

  1. Select the "Hands on Testing Java" project in the Package Explorer.
  2. Right-click on the project, and select the Run -> Run As -> JUnit Test.

This will look for all test-case classes in your project, and it will run them all for you.

Eclipse will open a JUnit window. On the top, you'll see how many errors you got as well as a red or green bar. Green means that all of the tests passed; red means that there's a problem.

If there's a problem, you'll get a Failure Trace at the bottom. Each line of the failure trace indicates what code was being executed when the error was discovered. Work yourself down this list to find the offending line of code (you'll rarely have to go past the first line). You'll have to debug your computation code (usually) to figure out what went wrong.