Introduzione al Testing in Java
Maria Milusheva
Senior Software Engineer
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)
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;
}
}
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.
@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);
}
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
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"));
}
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); }
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;
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);
}
La classe di test diventa:
@Test
void process_savesToInfoStore_whenInfoMessage() {
messageProcessor.process("[INFO] Process started.");
verify(infoStore).save("[INFO] Process started.");
verifyNoInteractions(errorStore);
}
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