Manuale Utente di JUnit

Questo manuale copre l'uso di JUnit 4 e JUnit 5, inclusa la scrittura dei test, le asserzioni, le suite di test e altre funzionalità principali.

Uso Base di JUnit 5

Scrittura delle Classi di Test

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class CalculatorTest {
    
    @Test
    void testAddition() {
        int result = 2 + 3;
        assertEquals(5, result);
    }
}

Asserzioni Comuni

import static org.junit.jupiter.api.Assertions.*;

// Equality assertions
assertEquals(expected, actual);
assertEquals(expected, actual, "Custom error message");

// Boolean assertions
assertTrue(condition);
assertFalse(condition);

// Null assertions
assertNull(object);
assertNotNull(object);

// Array assertions
assertArrayEquals(expectedArray, actualArray);

// Exception assertions
assertThrows(Exception.class, () -> {
    // Code that may throw exception
});

// Combined assertions
assertAll("Person properties",
    () -> assertEquals("John", person.getFirstName()),
    () -> assertEquals("Doe", person.getLastName())
);

Operazioni Before e After

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.AfterEach;

class MyTest {
    
    @BeforeEach
    void setUp() {
        // Runs before each test method
    }
    
    @AfterEach
    void tearDown() {
        // Runs after each test method
    }
}

Test Annidati

import org.junit.jupiter.api.Nested;

@Nested
class NestedTests {
    
    @Test
    void testInNestedClass() {
        // Tests in nested test class
    }
}

Test Parametrizzati

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

@ParameterizedTest
@ValueSource(ints = {1, 2, 3, 4, 5})
void testWithMultipleValues(int number) {
    assertTrue(number > 0);
}

Test Dinamici

import org.junit.jupiter.api.TestFactory;
import org.junit.jupiter.api.DynamicTest;
import java.util.Arrays;

@TestFactory
Collection<DynamicTest> dynamicTestsFromIterator() {
    return Arrays.asList(
        DynamicTest.dynamicTest("First dynamic test", () -> assertTrue(true)),
        DynamicTest.dynamicTest("Second dynamic test", () -> assertEquals(4, 2 + 2))
    );
}

Uso Base di JUnit 4

Scrittura delle Classi di Test

import org.junit.Test;
import static org.junit.Assert.*;

public class CalculatorTest {
    
    @Test
    public void testAddition() {
        assertEquals(5, 2 + 3);
    }
}

Asserzioni Comuni

// Equality assertions
assertEquals(expected, actual);
assertEquals("message", expected, actual);

// Boolean assertions
assertTrue(condition);
assertFalse(condition);

// Null assertions
assertNull(object);
assertNotNull(object);

// Exception assertions
@Test(expected = NullPointerException.class)
public void testException() {
    Object obj = null;
    obj.toString();
}

Before e After

import org.junit.Before;
import org.junit.After;

public class MyTest {
    
    @Before
    public void setUp() {
        // Runs before each test method
    }
    
    @After
    public void tearDown() {
        // Runs after each test method
    }
    
    @BeforeClass
    public static void setUpClass() {
        // Runs once before all tests in the class
    }
    
    @AfterClass
    public static void tearDownClass() {
        // Runs once after all tests in the class
    }
}

Suite di Test

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({Test1.class, Test2.class, Test3.class})
public class AllTests {
    // Runs all tests in Test1, Test2, and Test3
}

Migrazione da JUnit 4 a JUnit 5

Modifiche alle Annotazioni

JUnit 4JUnit 5
@Test@Test
@Before@BeforeEach
@After@AfterEach
@BeforeClass@BeforeAll
@AfterClass@AfterAll
@Ignore@Disabled
@SuiteClasses@SelectClasses

Modifiche alle Asserzioni

// JUnit 4
Assert.assertEquals(expected, actual);

// JUnit 5
Assertions.assertEquals(expected, actual);

Modifiche al Test delle Eccezioni

// JUnit 4
@Test(expected = NullPointerException.class)
public void testException() {
    Object obj = null;
    obj.toString();
}

// JUnit 5
@Test
public void testException() {
    assertThrows(NullPointerException.class, () -> {
        Object obj = null;
        obj.toString();
    });
}

Esecuzione dei Test

Riga di Comando

# Maven
mvn test

# Gradle
gradle test

# Standalone Console
java -jar junit-platform-console-standalone-5.14.3.jar -c MyTestClass

Esecuzione tramite IDE

  • IntelliJ IDEA: fare clic con il tasto destro sulla classe o sul metodo di test e selezionare Run
  • Eclipse: fare clic con il tasto destro sulla classe o sul metodo di test e selezionare Run As → JUnit Test
  • VS Code: utilizzare l'estensione JUnit per eseguire i test