“Do I need these instructions?”

You are welcome to follow these instructions to make things easier when you run the test cases while working on your assignments.

You will still need to run the Maven wrapper, either from the command line or from Eclipse, to make sure your assignment works according to the setup we specified.

With the following instructions, you are probably wondering “Why do I need to import the assignment as a Maven project, if all I want is to simply just test my project using JUnit?”

It’s because the project does not include the JUnit libraries (junit.jar, etc). But all this information is stored in the pom.xml file. The dependencies will be downloaded and imported into the project by Maven. Hence, we make a Maven project just so we have all our dependencies managed.

If you create a standard Java Project in Eclipse (File > New > Java Project), then Eclipse will complain about the test files (under src/test/java/...). It doesn’t know what JUnit is, and you would have to download and connect JUnit yourself. For this reason, we are making a Maven project so Eclipse knows to immediately look into the pom.xml, download JUnit, and connect it to our project.



Import the code into Eclipse as a Maven project

First, make sure you have cloned the code from GitHub to your computer.

If you have already imported the code as a Maven Project in Eclipse, you can jump to “Running the test cases using the JUnit GUI”.



From Eclipse, go to File, then Import...

Screenshot

Select Existing Maven Projects, then Next >

Screenshot

Click on Browse...

Screenshot

Screenshot

You will see the dialog populate, similar to this. Click Finish

Screenshot

Your Eclipse project has been created. The name of the project will depend on what’s in the <name> section of your pom.xml file.

Even though the project is recognised as a Maven project, we don’t want to use Eclipse’s built-in Maven feature. So, don’t use these Maven options. If you want to test everything works using the Maven wrapper provided to you, either use Maven wrapper command line instructions, or Maven wrapper with Eclipse instructions. This makes sure your assignment works according to the setup we specified.

Screenshot


Running the test cases using the JUnit GUI

For now, we just want to run the JUnit test cases. Open up the relevant Java test file by navigating in the Package Explorer, and double-click the file so it opens up.

Screenshot

Click the tiny arrow next to the main green run button, then Run As, then JUnit Test.

Screenshot

Select which tests you want to run. Usually you will select the entire Test Suite to run all tests, so select that:

Screenshot

You should see a JUnit panel open up (sometimes it opens up at the bottom instead of the left—but you can drag it wherever you want).

Notice the red bar signifying failed test cases. It turns green when they all pass.

You can double-click on a test case to jump to it in the code.

Nice and easy.

Screenshot