Java में Integration Testing
In this page:
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?
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());
}
}
Login to try C/C++/Java/PHP code in the editor
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
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());
}
}
Login to try C/C++/Java/PHP code in the editor
Testing Database Connections
Integration tests अक्सर specifically database connectivity verify करते हैं, यह जांचते हुए कि आपकी data access layer किसी mocked repository के बजाय एक असली (अक्सर temporary या containerized) database पर असल में records लिख, पढ़ और हटा सकती है।
उदाहरण: Testing Database Connections
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());
}
}
Login to try C/C++/Java/PHP code in the editor
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
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));
}
}
Login to try C/C++/Java/PHP code in the editor
Best Practices for Integration Tests
विश्वसनीय integration tests लिखने के लिए, हमेशा उन्हें production या shared infrastructure के बजाय एक अलग, isolated test environment के खिलाफ चलाएं, और सुनिश्चित करें कि हर test अपने बनाए किसी भी data को बाद में साफ़ कर दे, ताकि tests एक-दूसरे को प्रदूषित न करें या बचे-खुचे records न छोड़ें।
उदाहरण: Best Practices for Integration Tests
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);
}
}
Login to try C/C++/Java/PHP code in the editor
- integration tests को सीधे आपके live production database के अंदर चलाना, data corruption का जोखिम उठाते हुए।
- बहुत सारी intermediate classes mock करके unit tests को integration tests के साथ मिला देना, जिससे integration test बेकार हो जाता है।
- 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 हैं।
Chapter Quiz — Complete all 19 topics to unlock
0/19 topics done
Complete these topics first:
- Java Reflection API
- Java Annotations Advanced
- Java Garbage Collection
- Java Memory Management
- Java Performance Optimization
- Java Advanced Interview Questions
- Java CompletableFuture
- Java Atomic Classes
- Java Locks & Semaphores
- Java Concurrent Collections
- Java Cryptography Basics
- Java Hashing (MD5, SHA)
- Java SSL & HTTPS
- Java Logging (Log4j/SLF4J)
- Java Serialization Advanced
- Java Interview Questions Advanced
- Java Connection Pooling
- Java Test Driven Development
- Java Integration Testing