यूनिट टेस्ट: लिस्ट के साथ डेटाबेस
लेसन वाले ErrorStore और InfoStore पर विचार कीजिए, लेकिन अब हमारा process() मेथड संदेशों की एक लिस्ट स्वीकार कर रहा है. दी गई इनपुट लिस्ट के आधार पर जाँचें कि प्रत्येक डेटाबेस को सही संख्या में कॉल किया गया है.
यह अभ्यास पाठ्यक्रम का हिस्सा है
Java में Testing परिचय
अभ्यास निर्देश
InfoStoreके लिए एक मॉक बनाइए.ErrorStoreके लिए एक मॉक बनाइए.- मौजूदा आर्ग्युमेंट्स के साथ
InfoStoreमॉक को कितनी बार कॉल किया जाएगा, यह वेरीफाई कीजिए. - मौजूदा आर्ग्युमेंट्स के साथ
ErrorStoreमॉक को कितनी बार कॉल किया जाएगा, यह वेरीफाई कीजिए.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
public class Main {
public static void main(String[] args) {
launchMockitoTestsAndPrint(MessageProcessorTest.class);
}
}
class MessageProcessorTest {
@Test
void process_savesToCorrectDatabase_whenValidInputList() {
List sampleMessages = new ArrayList<>();
sampleMessages.add("[INFO] An info message.");
sampleMessages.add("[INFO] An info message.");
sampleMessages.add("[ERROR] An error message.");
sampleMessages.add("[INFO] An info message.");
// Create a mock for the InfoStore
InfoStore ____ = ____(____.class);
// Create a mock for the ErrorStore
ErrorStore ____ = ____(____.class);
MessageProcessor processor = new MessageProcessor(infoStore, errorStore);
processor.process(sampleMessages);
// Verify how many times the infoStore has to store the message
____(____, times(3)).save("[INFO] An info message.");
// Verify how many times the errorStore has to store the message
____(____, times(1)).save("[ERROR] An error message.");
}
}