← Back to Advanced Java Course | Chapter 10: Spring Boot | Lesson 5 of 6

Java Spring Boot की Testing

Spring Boot testing जांचती है कि आपके web application के हिस्से साथ काम करते हैं, जैसे show से पहले एक rehearsal। आप requests भेजने का नाटक कर सकते हैं और जवाब जांच सकते हैं।

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

java
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");
	}
}

Testing REST Controllers (MockMvc)

MockMvc आपको किसी test के अंदर अपने controllers के खिलाफ simulated HTTP requests करने देता है, पूरी request-handling pipeline (routing, validation, serialization) चलाते हुए बिना असल में कोई असली web server शुरू किए या कोई network socket खोले।

उदाहरण: Testing REST Controllers (MockMvc)

java
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");
	}
}

Mocking with @MockBean

@MockBean Spring का test-specific तरीका है किसी असली Bean, जैसे एक database repository, को किसी test द्वारा उपयोग किए application context के अंदर एक Mockito mock से बदलने का। यह आपको जिस controller या service को test किया जा रहा है उसे किसी ऐसी dependency से अलग करने देता है जिससे आप असल में टकराना नहीं चाहते।

उदाहरण: Mocking with @MockBean

java
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");
	}
}

Database Slice Testing

@DataJpaTest आपके application के सिर्फ JPA-related components को isolate करता है और अपने आप एक तेज़, in-memory database configuration boot करता है, जो सिर्फ repository query behavior validate करने के लिए पूरा application context चलाने से कहीं तेज़ है।

उदाहरण: Database Slice Testing

java
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");
	}
}

Mockito Verification Tests

Mockito की verification features आपको यह confirm करने देती हैं कि किसी test के दौरान कोई specific mocked method असल में call हुआ, और यह track करने देती हैं कि कितनी बार हुआ -- उन side effects (जैसे 'क्या email service invoke हुई') को assert करने के लिए उपयोगी जिन्हें सिर्फ एक return value उजागर नहीं करेगा।

उदाहरण: Mockito Verification Tests

java
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
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. हर test के लिए @SpringBootTest का उपयोग करना, जो पूरा application शुरू करता है और साधारण tests को धीमा बनाता है; जब संभव हो तब @WebMvcTest या @DataJpaTest जैसे slices का उपयोग करें।
  2. किसी mocked bean को stub करना भूल जाना, जिससे उसके methods null या default values return करते हैं और test एक भ्रामक तरीके से fail होता है।
  3. यह उम्मीद करना कि @DataJpaTest असली database का उपयोग करेगा, जबकि default रूप से यह एक embedded database उपयोग करता है और हर test को roll back करता है।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.