Java में Testing परिचय
Maria Milusheva
Senior Software Engineer
विचार करें:
List<Integer> newList = new ArrayList<Integer>();
newList.add(10);
newList.add(20);
newList.add(30);
तेज़ और छोटा तरीका:
List<Integer> newList = List.of(10, 20, 30);
Java Collections के Set और Map पर भी यही काम करता है
.of() से बने objects आम तौर पर immutable होते हैं (बदले नहीं जा सकते)
निम्न class देखें:
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;
}
}
हम किसी भी टाइप के कई args पास करने के लिए Arguments का उपयोग करते हैं.
Arguments - किसी भी तरह के किसी भी संख्या में objects रख सकता है.
उदाहरण:
Arguments.of(new Person("Monty", "Python"), "Monty Python");
अब इसे अगले @ParameterizedTest टाइप में उपयोग कर सकते हैं.
@MethodSource हमें किसी भी object को टेस्ट में पास करने देता है.
टेस्ट इस तरह दिखता है:
@ParameterizedTest
@MethodSource("provideNames")
void testFullName(Person person, String expectedFullName) {
assertEquals(person.fullName(person), expectedFullName);
}
@MethodSource के लिए method:
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;
}
नोट: method static होना चाहिए
नोट: कई return types मान्य हैं; List<Arguments> सबसे सरल है
@MethodSource के लिए method:
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"));
}
डेटाबेस वाला टेस्ट देखें:
@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); }
हम @BeforeEach annotation से ऐसा method बना सकते हैं जो हर टेस्ट से पहले चले:
import org.junit.jupiter.api.BeforeEach;
इसे उपयोग करने हेतु, पहले objects को fields के रूप में घोषित करें:
class MessageProcessorTest {
private InfoStore infoStore;
private ErrorStore errorStore;
private MessageProcessor messageProcessor;
प्रत्येक object एक अलग method में बनाएँ:
@BeforeEach
void setUp() {
this.infoStore = mock(InfoStore.class);
this.errorStore = mock(errorStore.class);
this.messageProcessor = new MessageProcessor(infoStore, errorStore);
}
इसके बाद test class इस तरह हो जाती है:
@Test
void process_savesToInfoStore_whenInfoMessage() {
messageProcessor.process("[INFO] Process started.");
verify(infoStore).save("[INFO] Process started.");
verifyNoInteractions(errorStore);
}
सभी एक साथ:
class MessageProcessorTest { private InfoStore infoStore; // Declare fields@BeforeEach void setUp() { this.infoStore = mock(InfoStore.class); // Initialize fields }@Test void process_savesToInfoStore_whenInfoMessage() { // Use fields } }
Java में Testing परिचय