Introducere în testarea în Java
Maria Milusheva
Senior Software Engineer
Considerați:
List<Integer> newList = new ArrayList<Integer>();
newList.add(10);
newList.add(20);
newList.add(30);
Variantă mai rapidă și concisă:
List<Integer> newList = List.of(10, 20, 30);
Funcționează la fel pentru Set și Map din Java Collections
Obiectele create cu .of() sunt de obicei imutabile (nu pot fi modificate)
Considerați clasa următoare:
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;
}
}
Folosim Arguments pentru a transmite mai multe argumente de orice tip.
Arguments - poate conține orice număr de obiecte de orice tip.
De exemplu:
Arguments.of(new Person("Monty", "Python"), "Monty Python");
Îl putem folosi acum în următorul tip @ParameterizedTest.
@MethodSource permite transmiterea oricărui obiect către un test.
Testul arată astfel:
@ParameterizedTest
@MethodSource("provideNames")
void testFullName(Person person, String expectedFullName) {
assertEquals(person.fullName(person), expectedFullName);
}
Metodă pentru @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;
}
Notă: metoda trebuie să fie static
Notă: există mai multe tipuri de returnare permise; List<Arguments> este cel mai simplu
Metodă pentru @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ți testul pentru baze de date:
@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); }
Putem folosi adnotarea @BeforeEach pentru a crea o metodă ce se execută înainte de fiecare test:
import org.junit.jupiter.api.BeforeEach;
Pentru a o utiliza, declarați mai întâi obiectele ca atribute:
class MessageProcessorTest {
private InfoStore infoStore;
private ErrorStore errorStore;
private MessageProcessor messageProcessor;
Creați fiecare obiect într-o metodă separată:
@BeforeEach
void setUp() {
this.infoStore = mock(InfoStore.class);
this.errorStore = mock(errorStore.class);
this.messageProcessor = new MessageProcessor(infoStore, errorStore);
}
Clasa de test devine:
@Test
void process_savesToInfoStore_whenInfoMessage() {
messageProcessor.process("[INFO] Process started.");
verify(infoStore).save("[INFO] Process started.");
verifyNoInteractions(errorStore);
}
Toate împreună:
class MessageProcessorTest { private InfoStore infoStore; // Declare fields@BeforeEach void setUp() { this.infoStore = mock(InfoStore.class); // Initialize fields }@Test void process_savesToInfoStore_whenInfoMessage() { // Use fields } }
Introducere în testarea în Java