← Back to Advanced Java Course | Chapter 11: Advanced & Security | Lesson 19 of 19

Java में Integration Testing

Integration testing यह जांचती है कि किसी program के अलग-अलग हिस्से साथ काम करते हैं, जैसे किसी bike के पुर्ज़े assemble करने के बाद यह जांचना कि पूरी bike चलती है या नहीं। यह हर हिस्से को अकेले जांचने से आगे जाती है।

What is Integration Testing?

Integration testing यह verify करती है कि कई स्वतंत्र modules, classes, या services -- database access layers, external APIs, internal components -- असल में एक unified system के रूप में सही तरीके से साथ काम करती हैं, उन coordination bugs को पकड़ते हुए जिन्हें हर टुकड़े को अलग से test करने से कभी पता नहीं चलता।

उदाहरण: What is Integration Testing?

java
public class Main {
	static class Database { String read() { return "data"; } }
	static class Service {
		Database db;
		Service(Database db) { this.db = db; }
		String process() { return "Processed: " + db.read(); } // real components wired together
	}
	public static void main(String[] args) {
		Service service = new Service(new Database());
		System.out.println(service.process());
	}
}

Unit vs. Integration Testing

Unit testing किसी single, isolated method या class को अलग से जांचती है, आमतौर पर उसकी dependencies के लिए mocks का उपयोग करते हुए। Integration testing जान-बूझकर उन mocks को छोड़कर असली components के बीच असली, end-to-end data flow चलाती है, धीमी और setup करने में मुश्किल होने की कीमत पर।

उदाहरण: Unit vs. Integration Testing

java
public class Main {
	interface Database { String read(); }
	static class FakeDatabase implements Database { public String read() { return "mocked"; } } // unit test: mock
	static class RealDatabase implements Database { public String read() { return "real data"; } } // integration: real flow
	public static void main(String[] args) {
		Database mock = new FakeDatabase();
		Database real = new RealDatabase();
		System.out.println(mock.read() + " vs " + real.read());
	}
}

Testing Database Connections

Integration tests अक्सर specifically database connectivity verify करते हैं, यह जांचते हुए कि आपकी data access layer किसी mocked repository के बजाय एक असली (अक्सर temporary या containerized) database पर असल में records लिख, पढ़ और हटा सकती है।

उदाहरण: Testing Database Connections

java
import java.util.*;
public class Main {
	static class FakeDb {
		List<String> rows = new ArrayList<>();
		void insert(String name) { rows.add(name); } // simulates a real INSERT
		List<String> selectAll() { return rows; }
	}
	public static void main(String[] args) {
		FakeDb db = new FakeDb(); // stand-in for a real, temporary test database
		db.insert("Riya");
		System.out.println(db.selectAll());
	}
}

Testing API Client Integrations

Integration tests external APIs से connections को भी verify कर सकते हैं, यह पुष्टि करते हुए कि आपका application असली या realistic endpoints से सही तरीके से requests भेजता है और responses parse करता है -- उदाहरण के लिए, cookiescursor.com पर आपके code की निर्भर third-party services।

उदाहरण: Testing API Client Integrations

java
public class Main {
	interface ApiClient { String fetchUser(int id); }
	static class RealApiClient implements ApiClient {
		public String fetchUser(int id) { return "{\"id\":" + id + ",\"name\":\"Riya\"}"; } // realistic response shape
	}
	public static void main(String[] args) {
		ApiClient client = new RealApiClient();
		System.out.println(client.fetchUser(1));
	}
}

Best Practices for Integration Tests

विश्वसनीय integration tests लिखने के लिए, हमेशा उन्हें production या shared infrastructure के बजाय एक अलग, isolated test environment के खिलाफ चलाएं, और सुनिश्चित करें कि हर test अपने बनाए किसी भी data को बाद में साफ़ कर दे, ताकि tests एक-दूसरे को प्रदूषित न करें या बचे-खुचे records न छोड़ें।

उदाहरण: Best Practices for Integration Tests

java
import java.util.*;
public class Main {
	static List<String> testData = new ArrayList<>();
	public static void main(String[] args) {
		testData.add("temp-record"); // created in an isolated test environment
		System.out.println("Before cleanup: " + testData);
		testData.clear(); // cleaned up afterward so it doesn't pollute other tests
		System.out.println("After cleanup: " + testData);
	}
}
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. integration tests को सीधे आपके live production database के अंदर चलाना, data corruption का जोखिम उठाते हुए।
  2. बहुत सारी intermediate classes mock करके unit tests को integration tests के साथ मिला देना, जिससे integration test बेकार हो जाता है।
  3. tests चलाने के बाद databases में गंदे test records छोड़ देना, जिससे बाद के tests fail होते हैं।
चैप्टर सारांश
  • Integration testing verify करती है कि कई स्वतंत्र modules, classes, या database services कैसे coordinate करते और साथ काम करते हैं।
  • Unit testing के विपरीत, integration tests असली systems और databases में end-to-end data flows verify करते हैं।
  • हमेशा integration tests को एक अलग environment में चलाएं, और test pollution रोकने के लिए पूरा होने पर test databases साफ़ करें।
ब्राउज़र सपोर्ट

Standard Java system integration और I/O streams सभी Java development kits द्वारा natively supported हैं।

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.