Java Spring Boot की Testing
In this page:
Integration Testing in Spring Boot
Integration tests यह verify करते हैं कि किसी Spring Boot application के कई हिस्से -- database layers, configuration, और API controllers -- असल में एक realistic, wired-up context में सही तरीके से साथ काम करते हैं, उन bugs को पकड़ते हुए जिन्हें mocked dependencies वाले isolated unit tests पूरी तरह छोड़ देते।
उदाहरण: Integration Testing in Spring Boot
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest // wires up the full, realistic application context
public class Main {
public static void main(String[] args) {
System.out.println("Integration tests catch bugs isolated unit tests would miss");
}
}
Login to try C/C++/Java/PHP code in the editor
Testing REST Controllers (MockMvc)
MockMvc आपको किसी test के अंदर अपने controllers के खिलाफ simulated HTTP requests करने देता है, पूरी request-handling pipeline (routing, validation, serialization) चलाते हुए बिना असल में कोई असली web server शुरू किए या कोई network socket खोले।
उदाहरण: Testing REST Controllers (MockMvc)
import org.springframework.test.web.servlet.MockMvc;
public class Main {
public static void main(String[] args) {
// mockMvc.perform(get("/users/1")).andExpect(status().isOk());
System.out.println("MockMvc simulates HTTP requests without a real server");
}
}
Login to try C/C++/Java/PHP code in the editor
Mocking with @MockBean
@MockBean Spring का test-specific तरीका है किसी असली Bean, जैसे एक database repository, को किसी test द्वारा उपयोग किए application context के अंदर एक Mockito mock से बदलने का। यह आपको जिस controller या service को test किया जा रहा है उसे किसी ऐसी dependency से अलग करने देता है जिससे आप असल में टकराना नहीं चाहते।
उदाहरण: Mocking with @MockBean
import org.springframework.boot.test.mock.mockito.MockBean;
public class Main {
interface UserRepository { String findById(int id); }
@MockBean
UserRepository userRepository; // swaps the real Bean for a Mockito mock in the test context
public static void main(String[] args) {
System.out.println("@MockBean isolates the class under test from a real dependency");
}
}
Login to try C/C++/Java/PHP code in the editor
Database Slice Testing
@DataJpaTest आपके application के सिर्फ JPA-related components को isolate करता है और अपने आप एक तेज़, in-memory database configuration boot करता है, जो सिर्फ repository query behavior validate करने के लिए पूरा application context चलाने से कहीं तेज़ है।
उदाहरण: Database Slice Testing
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
@DataJpaTest // boots only the JPA layer with a fast in-memory database
public class Main {
public static void main(String[] args) {
System.out.println("Faster than booting the full application context");
}
}
Login to try C/C++/Java/PHP code in the editor
Mockito Verification Tests
Mockito की verification features आपको यह confirm करने देती हैं कि किसी test के दौरान कोई specific mocked method असल में call हुआ, और यह track करने देती हैं कि कितनी बार हुआ -- उन side effects (जैसे 'क्या email service invoke हुई') को assert करने के लिए उपयोगी जिन्हें सिर्फ एक return value उजागर नहीं करेगा।
उदाहरण: Mockito Verification Tests
import static org.mockito.Mockito.*;
public class Main {
interface EmailService { void send(String to); }
public static void main(String[] args) {
EmailService mockEmail = mock(EmailService.class);
mockEmail.send("[email protected]");
verify(mockEmail).send("[email protected]"); // confirms the side effect happened
}
}
Login to try C/C++/Java/PHP code in the editor
- हर test के लिए
@SpringBootTestका उपयोग करना, जो पूरा application शुरू करता है और साधारण tests को धीमा बनाता है; जब संभव हो तब@WebMvcTestया@DataJpaTestजैसे slices का उपयोग करें। - किसी mocked bean को stub करना भूल जाना, जिससे उसके methods
nullया default values return करते हैं और test एक भ्रामक तरीके से fail होता है। - यह उम्मीद करना कि
@DataJpaTestअसली database का उपयोग करेगा, जबकि default रूप से यह एक embedded database उपयोग करता है और हर test को roll back करता है।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: