Annotazioni aggiuntive

Introduzione al Testing in Java

Maria Milusheva

Senior Software Engineer

Prerequisito: creazione oggetti con .of()

Considera:

List<Integer> newList = new ArrayList<Integer>();
newList.add(10);
newList.add(20);
newList.add(30);

Modo più rapido e corto:

List<Integer> newList = List.of(10, 20, 30);
  • Vale anche per Set e Map delle Java Collections

  • Gli oggetti creati con .of() sono di solito immutabili (non modificabili)

Introduzione al Testing in Java

Passare oggetti a @ParameterizedTest

Considera la seguente classe:

class Person {
    String firstName;
    String lastName;
    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
    public String fullName(Person person) {
        return firstName + " " + lastName;
    }
}
Introduzione al Testing in Java

Classe Arguments

Usiamo Arguments per passare più argomenti di qualsiasi tipo.

Arguments: può contenere qualsiasi numero di oggetti di qualunque tipo.

Per esempio:

Arguments.of(new Person("Monty", "Python"), "Monty Python");

Ora possiamo usarlo nel prossimo tipo di @ParameterizedTest.

Introduzione al Testing in Java

@MethodSource

@MethodSource permette di passare qualsiasi oggetto a un test.

Il test è così:

@ParameterizedTest
@MethodSource("provideNames")
void testFullName(Person person, String expectedFullName) {
    assertEquals(person.fullName(person), expectedFullName);
}
Introduzione al Testing in Java

Metodo provider @MethodSource

Metodo per @MethodSource:

private static List<Arguments> provideNames() {
    List<Arguments> args = new ArrayList<>();
    args.add(Arguments.of(new Person("Robert", "Martin"), "Robert Martin"));
    args.add(Arguments.of(new Person("Heinz", "Kabutz"), "Heinz Kabutz"));

    return args;
}

Nota: il metodo deve essere static

Nota: sono permessi molti tipi di ritorno; List<Arguments> è il più semplice

1 https://junit.org/junit5/docs/5.2.0/api/org/junit/jupiter/params/provider/MethodSource.html
Introduzione al Testing in Java

Metodo provider @MethodSource

Metodo per @MethodSource:

private static List<Arguments> provideNames() {
    return List.of(
        Arguments.of(new Person("John", "Doe"), "John Doe"),
        Arguments.of(new Person("Jane", "Doe"), "Jane Doe"),
        Arguments.of(new Person("Alice", "Bob"), "Alice Bob"));
}
Introduzione al Testing in Java

E se servono più dei soli argomenti?

Considera il test dei database:

@Test
void process_savesToInfoStore_whenInfoMessage() {
  InfoStore infoStore = mock(InfoStore.class);
  ErrorStore errorStore = mock(ErrorStore.class);
  MessageProcessor messageProcessor = new MessageProcessor(infoStore, errorStore);

messageProcessor.saveMessage("[INFO] Process started.");
verify(infoStore).save("[INFO] Process started."); verifyNoInteractions(errorStore); }
Introduzione al Testing in Java

Annotazione @BeforeEach

Possiamo usare l’annotazione @BeforeEach per creare un metodo eseguito prima di ogni test:

import org.junit.jupiter.api.BeforeEach;

Per usarla, dichiara prima gli oggetti come campi:

class MessageProcessorTest {

    private InfoStore infoStore;
    private ErrorStore errorStore;
    private MessageProcessor messageProcessor;
Introduzione al Testing in Java

Metodo per @BeforeEach

Crea ogni oggetto in un metodo separato:

@BeforeEach
void setUp() {
    this.infoStore = mock(InfoStore.class);
    this.errorStore = mock(errorStore.class);
    this.messageProcessor = new MessageProcessor(infoStore, errorStore);
}
Introduzione al Testing in Java

Test abbreviato

La classe di test diventa:

@Test
void process_savesToInfoStore_whenInfoMessage() {
    messageProcessor.process("[INFO] Process started.");

    verify(infoStore).save("[INFO] Process started."); 
    verifyNoInteractions(errorStore); 
}
Introduzione al Testing in Java

Flusso completo di @BeforeEach

Tutto insieme:

class MessageProcessorTest {
    private InfoStore infoStore; // Dichiara i campi


@BeforeEach void setUp() { this.infoStore = mock(InfoStore.class); // Inizializza i campi }
@Test void process_savesToInfoStore_whenInfoMessage() { // Usa i campi } }
Introduzione al Testing in Java

Passons à la pratique !

Introduzione al Testing in Java

Preparing Video For Download...