Java'da Teste Giriş
Maria Milusheva
Senior Software Engineer
Şunu düşünün:
List<Integer> newList = new ArrayList<Integer>();
newList.add(10);
newList.add(20);
newList.add(30);
Bunun yerine daha hızlı ve kısa yol:
List<Integer> newList = List.of(10, 20, 30);
Java Collections’taki Set ve Map için de aynı şekilde çalışır
.of() ile oluşturulan nesneler genelde değiştirilemez (immutable)
Aşağıdaki sınıfı düşünün:
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;
}
}
Her türden birden çok argümanı geçirmek için Arguments kullanırız.
Arguments — herhangi türde, herhangi sayıda nesne içerebilir.
Örneğin:
Arguments.of(new Person("Monty", "Python"), "Monty Python");
Şimdi bunu bir sonraki @ParameterizedTest türünde kullanabiliriz.
@MethodSource, herhangi bir nesneyi teste geçirmemizi sağlar.
Test şu şekildedir:
@ParameterizedTest
@MethodSource("provideNames")
void testFullName(Person person, String expectedFullName) {
assertEquals(person.fullName(person), expectedFullName);
}
@MethodSource için yöntem:
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: yöntem static olmalıdır
Not: birçok dönüş türü kabul edilir; List<Arguments> en basitidir
@MethodSource için yöntem:
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"));
}
Veritabanları testini düşünün:
@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); }
Her testten önce çalışan bir yöntem oluşturmak için @BeforeEach ek açıklamasını kullanabiliriz:
import org.junit.jupiter.api.BeforeEach;
Kullanmak için önce nesneleri alan olarak bildirin:
class MessageProcessorTest {
private InfoStore infoStore;
private ErrorStore errorStore;
private MessageProcessor messageProcessor;
Her nesneyi ayrı bir yöntemde oluşturun:
@BeforeEach
void setUp() {
this.infoStore = mock(InfoStore.class);
this.errorStore = mock(errorStore.class);
this.messageProcessor = new MessageProcessor(infoStore, errorStore);
}
Test sınıfı şu hale gelir:
@Test
void process_savesToInfoStore_whenInfoMessage() {
messageProcessor.process("[INFO] Process started.");
verify(infoStore).save("[INFO] Process started.");
verifyNoInteractions(errorStore);
}
Hepsi bir arada:
class MessageProcessorTest { private InfoStore infoStore; // Alanları bildir@BeforeEach void setUp() { this.infoStore = mock(InfoStore.class); // Alanları başlat }@Test void process_savesToInfoStore_whenInfoMessage() { // Alanları kullan } }
Java'da Teste Giriş