Writing Unit Tests in Java Setup

Setup

Launch the IntelliJ IDEA application.

Select the New Project option.

Name the project writing-unit-tests-in-java.

Create the project in the ~/code/ga/labs directory.

⚠️ Windows users, the labs directory is likely at this location on your device: C:\Users\username\code\ga\labs (replacing username with your username). Create the project in that directory instead.

Choose the Maven build system option.

Ensure the JDK used is temurin-17. This should be the default setting when starting a new project. If it’s not, alert your instructor.

Ensure that the Add sample code option is not checked.

After confirming that the New Project window looks similar to the screenshot below, select the Create button. The name of the project should be writing-unit-tests-in-java, not untitled.

A new project being created in the ~/code/ga/labs directory, the JDK is temurin-17, and the add sample code option is has been unchecked. The name of the project should be writing-unit-tests-in-java.

Right-click on the src/main/java directory and select New > Java Class, as shown in the screenshot below:

The beginnings of a new Java class in the src/main/java directory.

Name your new class org.example.Calculator as shown below.

The org.example.Calculator class being created.

When your New Java Class prompt looks like the above, select the Class option from the list. You should be taken to the new Calculator.java file.

Add the required main() method inside of the Calculator class, including a print statement to confirm our setup worked:

    public static void main(String[] args) {
        System.out.println("This works!");
    }

Run this file now to test that our setup worked. Use the green play button in the top bar, or use Ctrl + R.

Run the Main.java file

A Run panel will appear at the bottom of the window, and you should see a message: This works!

We can get to work now that our project is set up.