Java में Test Driven Development
In this page:
What is Test Driven Development?
Test Driven Development (TDD) एक development process है जहां आप production code लिखने से पहले एक automated test लिखते हैं जो उसे pass करवाता है। यह एक सख्त Red-Green-Refactor cycle का पालन करता है, जो आपको implementation शुरू करने से पहले किसी feature के expected behavior के बारे में सोचने पर मजबूर करता है।
उदाहरण: What is Test Driven Development?
public class Main {
public static void main(String[] args) {
// Red -> Green -> Refactor
System.out.println("Write the test before the production code that makes it pass");
}
}
Login to try C/C++/Java/PHP code in the editor
The Red Phase
Red Phase पहला step है: आप एक ऐसे feature के लिए test लिखते हैं जो अभी मौजूद नहीं, और पुष्टि करते हैं कि यह fail होता है। यह दोनों स्पष्ट करता है कि feature को वास्तव में क्या करना चाहिए और यह साबित करता है कि test असल में failure पकड़ने में सक्षम है, बजाय बेवजह pass होने के।
उदाहरण: The Red Phase
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class Main {
static int add(int a, int b) { return 0; } // not implemented yet -- test should fail
@Test
void testAdd() {
assertEquals(5, add(2, 3)); // fails: proves the test can actually catch a wrong result
}
public static void main(String[] args) {
System.out.println("Red: test written, expected to fail against the stub");
}
}
Login to try C/C++/Java/PHP code in the editor
The Green Phase
Green Phase अगला है: आप fail हो रहे test को pass करवाने के लिए ज़रूरी production code की न्यूनतम मात्रा लिखते हैं, भले ही वह code अभी elegant या पूरी तरह optimized न हो। यहां लक्ष्य पहले correctness है, polish नहीं।
उदाहरण: The Green Phase
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class Main {
static int add(int a, int b) { return a + b; } // minimal code to pass, not yet polished
@Test
void testAdd() {
assertEquals(5, add(2, 3));
}
public static void main(String[] args) {
new Main().testAdd();
System.out.println("Green: test now passes");
}
}
Login to try C/C++/Java/PHP code in the editor
The Refactor Phase
Refactor Phase वह जगह है जहां आप अभी-अभी लिखा code साफ़ करते हैं, सरल बनाते हैं, और फिर से व्यवस्थित करते हैं, साथ ही यह पुष्टि करने के लिए लगातार test suite चलाते रहते हैं कि आपके बदलावों ने रास्ते में किसी मौजूदा behavior को नहीं तोड़ा।
उदाहरण: The Refactor Phase
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class Main {
static int add(int... numbers) { // cleaned up to accept any count of numbers
int sum = 0;
for (int n : numbers) sum += n;
return sum;
}
@Test
void testAdd() {
assertEquals(5, add(2, 3)); // still passes after refactoring
}
public static void main(String[] args) {
new Main().testAdd();
System.out.println("Refactor: cleaned up, tests still green");
}
}
Login to try C/C++/Java/PHP code in the editor
TDD for Web Utilities
TDD मज़बूत, reusable utilities बनाने के लिए खासतौर पर उपयोगी है: cookiescursor.com जैसी site पर चल रही background calculations के लिए पहले tests लिखना सुनिश्चित करता है कि वे calculations production तक पहुंचने से पहले पूरी तरह verified हों और चुपचाप regress होने की संभावना कम हो।
उदाहरण: TDD for Web Utilities
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class Main {
static double discount(double price, double percent) { return price - (price * percent / 100); }
@Test
void testDiscount() {
assertEquals(90.0, discount(100.0, 10.0)); // background calculation verified before shipping
}
public static void main(String[] args) {
new Main().testDiscount();
System.out.println("Discount calculation verified via test-first development");
}
}
Login to try C/C++/Java/PHP code in the editor
- कोई भी automated tests लिखने से पहले production code के बड़े हिस्से लिखना, TDD guidelines का उल्लंघन करते हुए।
- Refactor Phase को skip करना, जो समय के साथ गंदे और maintain करने में मुश्किल codebases की ओर ले जाता है।
- ऐसे tests लिखना जो बहुत जटिल हों या एक साथ बहुत सी चीज़ें जांचें, जिससे bugs को isolate करना मुश्किल हो जाता है।
- Test Driven Development एक सख्त Red-Green-Refactor development cycle है।
- पहले एक fail होने वाला test लिखें (Red), उसे pass करने के लिए न्यूनतम code implement करें (Green), और tests को green रखते हुए code optimize करें (Refactor)।
- TDD अत्यधिक reliable code सुनिश्चित करता है, regression bugs रोकता है, और आपकी software design architecture सुधारता है।
Standard Java testing और assertion structures सभी 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