Aanvullende annotaties

Introductie tot testen in Java

Maria Milusheva

Senior Software Engineer

Vooraf: objecten maken met .of()

Bekijk:

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

Sneller en korter alternatief:

List<Integer> newList = List.of(10, 20, 30);
  • Werkt hetzelfde voor Set en Map uit Java Collections

  • Objecten gemaakt met .of() zijn meestal immutable (niet wijzigbaar)

Introductie tot testen in Java

Objecten doorgeven aan @ParameterizedTest

Bekijk de volgende klasse:

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;
    }
}
Introductie tot testen in Java

Arguments-klasse

We gebruiken Arguments om meerdere args van elk type door te geven.

Arguments kan elk aantal objecten van om het even welk type bevatten.

Bijv.:

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

We kunnen dit nu gebruiken in het volgende type @ParameterizedTest.

Introductie tot testen in Java

@MethodSource

@MethodSource laat ons willekeurige objecten aan een test doorgeven.

De test ziet er zo uit:

@ParameterizedTest
@MethodSource("provideNames")
void testFullName(Person person, String expectedFullName) {
    assertEquals(person.fullName(person), expectedFullName);
}
Introductie tot testen in Java

@MethodSource-providermethode

Methode voor @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;
}

Let op: methode moet static zijn

Let op: veel toegestane returntypes; List<Arguments> is het eenvoudigst

1 https://junit.org/junit5/docs/5.2.0/api/org/junit/jupiter/params/provider/MethodSource.html
Introductie tot testen in Java

@MethodSource-providermethode

Methode voor @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"));
}
Introductie tot testen in Java

Wat als we meer dan argumenten nodig hebben?

Bekijk de databasetest:

@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); }
Introductie tot testen in Java

Annotatie @BeforeEach

We kunnen de annotatie @BeforeEach gebruiken om een methode te maken die vóór elke test draait:

import org.junit.jupiter.api.BeforeEach;

Om die te gebruiken, declareer objecten eerst als velden:

class MessageProcessorTest {

    private InfoStore infoStore;
    private ErrorStore errorStore;
    private MessageProcessor messageProcessor;
Introductie tot testen in Java

Methode voor @BeforeEach

Maak elk object aan in een aparte methode:

@BeforeEach
void setUp() {
    this.infoStore = mock(InfoStore.class);
    this.errorStore = mock(errorStore.class);
    this.messageProcessor = new MessageProcessor(infoStore, errorStore);
}
Introductie tot testen in Java

Ingekorte test

De testklasse wordt dan:

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

    verify(infoStore).save("[INFO] Process started."); 
    verifyNoInteractions(errorStore); 
}
Introductie tot testen in Java

Volledige @BeforeEach-flow

Alles bij elkaar:

class MessageProcessorTest {
    private InfoStore infoStore; // Velden declareren


@BeforeEach void setUp() { this.infoStore = mock(InfoStore.class); // Velden initialiseren }
@Test void process_savesToInfoStore_whenInfoMessage() { // Velden gebruiken } }
Introductie tot testen in Java

Laten we oefenen!

Introductie tot testen in Java

Preparing Video For Download...