Contoh 1: Test Simple Calculation
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;
}
public int multiply(int a, int b) {
return a * b;
}
public int divide(int a, int b) {
if (b == 0) throw new IllegalArgumentException("Cannot divide by zero");
return a / b;
}
public boolean isEven(int n) {
return n % 2 == 0;
}
}
File: src/test/java/com/example/TestCalculator.java
package com.example;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
import static org.junit.jupiter.api.Assertions.*;
public class TestCalculator {
private Calculator calc;
@BeforeEach
public void setup() {
calc = new Calculator();
}
@Test
public void testAdd() {
assertEquals(5, calc.add(2, 3));
System.out.println("✅ testAdd passed");
}
@Test
public void testSubtract() {
assertEquals(2, calc.subtract(5, 3));
System.out.println("✅ testSubtract passed");
}
@Test
public void testMultiply() {
assertEquals(12, calc.multiply(3, 4));
System.out.println("✅ testMultiply passed");
}
@Test
public void testDivide() {
assertEquals(2, calc.divide(6, 3));
System.out.println("✅ testDivide passed");
}
@Test
public void testDivideByZero() {
assertThrows(IllegalArgumentException.class, () -> {
calc.divide(10, 0);
});
System.out.println("✅ testDivideByZero passed");
}
@Test
public void testIsEven() {
assertTrue(calc.isEven(4));
assertFalse(calc.isEven(5));
System.out.println("✅ testIsEven passed");
}
}
Cara Jalankan
Option 1: Klik Right-Click di file → “Run Tests”
VSCode akan menjalankan semua test methods dan show hasil.
Option 2: Gunakan Terminal
mvn test -Dtest=TestCalculator
Expected Output
✅ testAdd passed
✅ testSubtract passed
✅ testMultiply passed
✅ testDivide passed
✅ testDivideByZero passed
✅ testIsEven passed
Tests run: 6, Failures: 0, Skipped: 0, Time elapsed: 0.xxx s
Contoh 2: Test dengan Setup dan Cleanup
File: src/test/java/com/example/TestWithSetup.java
package com.example;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.AfterEach;
import static org.junit.jupiter.api.Assertions.*;
import java.util.*;
public class TestWithSetup {
private List<String> list;
@BeforeEach
public void setup() {
list = new ArrayList<>();
System.out.println("Setup: List created");
}
@Test
public void testAddItem() {
list.add("Apple");
assertEquals(1, list.size());
System.out.println(" ✅ testAddItem passed");
}
@Test
public void testRemoveItem() {
list.add("Apple");
list.add("Banana");
list.remove("Apple");
assertEquals(1, list.size());
System.out.println(" ✅ testRemoveItem passed");
}
@Test
public void testListEmpty() {
assertTrue(list.isEmpty());
System.out.println(" ✅ testListEmpty passed");
}
@AfterEach
public void cleanup() {
list = null;
System.out.println("Cleanup: List cleared\n");
}
}
Output:
Setup: List created
✅ testAddItem passed
Cleanup: List cleared
Setup: List created
✅ testRemoveItem passed
Cleanup: List cleared
Setup: List created
✅ testListEmpty passed
Cleanup: List cleared
Tests run: 3, Failures: 0
Perhatikan: Setup dan cleanup dipanggil 3 kali untuk 3 test methods.