Here's a step-by-step guide to creating a simple Java project in IntelliJ IDEA that uses JUnit 5 for testing. The instructions are clear, detailed, and directly implementable — ideal for beginners or those new to IntelliJ and JUnit 5.
🧰 Prerequisites
- You have IntelliJ IDEA Community Edition (or Ultimate) installed.
- You have the Java SDK (JDK) installed (preferably JDK 8 or higher).
- Ensure that JUnit 5 is available (comes bundled with modern IntelliJ versions).
✅ Step 1: Create a New Project
- Open IntelliJ IDEA.
- Click on "New Project".
- In the "New Project" window:
- Select Java from the left panel.
- Choose "Command Line App" (or just a basic Java project if it's not available).
- Ensure the correct Project SDK is selected (e.g., JDK 17).
- Click Next → Next → Finish.
This will create a project with a basic structure.
📁 Step 2: Understand the Project Structure
After creation, your project should look like this:
MyFirstJUnit5Project/
├── src/
│ └── Main.java
├── out/ (compiled classes)
└── MyFirstJUnit5Project.iml
But we need to follow standard Maven-like structure for test separation:
We'll manually restructure it to:
MyFirstJUnit5Project/
├── src/
│ ├── main/
│ │ └── java/
│ │ └── com/
│ │ └── example/
│ │ ├── Calculator.java
│ │ └── Main.java
│ └── test/
│ └── java/
│ └── com/
│ └── example/
│ └── CalculatorTest.java
└── .idea/
🔨 Step 3: Set Up Correct Source Folders
- Right-click on
src/main/java → Mark Directory as → Sources Root
- Right-click on
src/test/java → Mark Directory as → Test Sources Root
This helps IntelliJ recognize where production code and test code live.
💻 Step 4: Write Some Java Code
➤ Create package folder:
Inside src/main/java, create a folder named com, then inside it another named example.
So you’ll have:
src/main/java/com/example/
➤ Create Calculator.java
// File: src/main/java/com/example/Calculator.java
package com.example;
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public int subtract(int a, int b) {
return a - b;
}
}
➤ Create Main.java
// File: src/main/java/com/example/Main.java
package com.example;
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println("Addition: " + calc.add(5, 3));
System.out.println("Subtraction: " + calc.subtract(10, 4));
}
}
🧪 Step 5: Add JUnit 5 Dependency (for non-Maven projects)
If you didn't create a Maven project, you need to manually add JUnit 5 libraries.
➤ Go to:
File → Project Structure → Libraries → Click + → From Maven...
Search for:
org.junit.jupiter:junit-jupiter:5.10.0
Click OK → Choose your module → Apply
This adds all required JUnit 5 dependencies.
🧪 Step 6: Write Your First JUnit 5 Test
➤ Create package folder for tests:
In src/test/java, create folders:
com → example
So path becomes:
src/test/java/com/example/
➤ Create CalculatorTest.java
// File: src/test/java/com/example/CalculatorTest.java
package com.example;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
class CalculatorTest {
private Calculator calculator;
@BeforeEach
void setUp() {
calculator = new Calculator();
}
@Test
void testAdd() {
int result = calculator.add(2, 3);
assertEquals(5, result, "2 + 3 should equal 5");
}
@Test
void testSubtract() {
int result = calculator.subtract(10, 5);
assertEquals(5, result, "10 - 5 should equal 5");
}
@AfterEach
void tearDown() {
calculator = null;
}
}
▶️ Step 7: Run the Tests
- Right-click anywhere inside the
CalculatorTest.java file.
- Choose Run 'CalculatorTest'.
- You should see green checkmarks indicating success.
🚀 Optional: Run the Main Class
- Right-click inside
Main.java and select Run 'Main.main()'.
- It should print:
Addition: 8
Subtraction: 6
✅ Summary
You now have a working Java project with:
- A proper directory structure (
main and test separated).
- A simple class (
Calculator) with two methods.
- A JUnit 5 test class that tests both methods.
- All setup done without Maven or Gradle, suitable for small standalone apps.
Let me know if you want to do the same using Maven or Gradle, which is more common in real-world projects!