Wprowadzenie do testowania w Javie
Maria Milusheva
Senior Software Engineer
Rozważmy:
List<Integer> newList = new ArrayList<Integer>();
newList.add(10);
newList.add(20);
newList.add(30);
Krótszy sposób:
List<Integer> newList = List.of(10, 20, 30);
Działa tak samo dla Set i Map z Java Collections
Obiekty tworzone przez .of() są zwykle niemutowalne (nie można ich zmieniać)
Rozważmy następującą klasę:
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;
}
}
Klasa Arguments służy do przekazywania wielu argumentów dowolnego typu.
Arguments — może zawierać dowolną liczbę obiektów dowolnego rodzaju.
Na przykład:
Arguments.of(new Person("Monty", "Python"), "Monty Python");
Można jej teraz użyć w kolejnym typie @ParameterizedTest.
@MethodSource umożliwia przekazanie dowolnego obiektu do testu.
Test wygląda następująco:
@ParameterizedTest
@MethodSource("provideNames")
void testFullName(Person person, String expectedFullName) {
assertEquals(person.fullName(person), expectedFullName);
}
Metoda dla @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;
}
Uwaga: metoda musi być static
Uwaga: dozwolonych jest wiele typów zwracanych; List<Arguments> jest najprostszym
Metoda dla @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"));
}
Rozważmy test dla baz danych:
@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); }
Adnotacja @BeforeEach tworzy metodę wykonywaną przed każdym testem:
import org.junit.jupiter.api.BeforeEach;
Najpierw zadeklaruj obiekty jako pola:
class MessageProcessorTest {
private InfoStore infoStore;
private ErrorStore errorStore;
private MessageProcessor messageProcessor;
Utwórz każdy obiekt w osobnej metodzie:
@BeforeEach
void setUp() {
this.infoStore = mock(InfoStore.class);
this.errorStore = mock(errorStore.class);
this.messageProcessor = new MessageProcessor(infoStore, errorStore);
}
Klasa testowa przyjmuje postać:
@Test
void process_savesToInfoStore_whenInfoMessage() {
messageProcessor.process("[INFO] Process started.");
verify(infoStore).save("[INFO] Process started.");
verifyNoInteractions(errorStore);
}
Wszystko razem:
class MessageProcessorTest { private InfoStore infoStore; // Declare fields@BeforeEach void setUp() { this.infoStore = mock(InfoStore.class); // Initialize fields }@Test void process_savesToInfoStore_whenInfoMessage() { // Use fields } }
Wprowadzenie do testowania w Javie