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

Java में Test Driven Development

Test driven development का मतलब है पहले एक test लिखना, फिर उसे pass करने के लिए बस उतना ही code लिखना, फिर उसे संवारना, जैसे solve करने से पहले अपने homework का जवाब जांचना। आप इस cycle को छोटे steps में दोहराते हैं।

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?

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

The Red Phase

Red Phase पहला step है: आप एक ऐसे feature के लिए test लिखते हैं जो अभी मौजूद नहीं, और पुष्टि करते हैं कि यह fail होता है। यह दोनों स्पष्ट करता है कि feature को वास्तव में क्या करना चाहिए और यह साबित करता है कि test असल में failure पकड़ने में सक्षम है, बजाय बेवजह pass होने के।

उदाहरण: The Red Phase

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

The Green Phase

Green Phase अगला है: आप fail हो रहे test को pass करवाने के लिए ज़रूरी production code की न्यूनतम मात्रा लिखते हैं, भले ही वह code अभी elegant या पूरी तरह optimized न हो। यहां लक्ष्य पहले correctness है, polish नहीं।

उदाहरण: The Green Phase

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

The Refactor Phase

Refactor Phase वह जगह है जहां आप अभी-अभी लिखा code साफ़ करते हैं, सरल बनाते हैं, और फिर से व्यवस्थित करते हैं, साथ ही यह पुष्टि करने के लिए लगातार test suite चलाते रहते हैं कि आपके बदलावों ने रास्ते में किसी मौजूदा behavior को नहीं तोड़ा।

उदाहरण: The Refactor Phase

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

TDD for Web Utilities

TDD मज़बूत, reusable utilities बनाने के लिए खासतौर पर उपयोगी है: cookiescursor.com जैसी site पर चल रही background calculations के लिए पहले tests लिखना सुनिश्चित करता है कि वे calculations production तक पहुंचने से पहले पूरी तरह verified हों और चुपचाप regress होने की संभावना कम हो।

उदाहरण: TDD for Web Utilities

java
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");
	}
}
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. कोई भी automated tests लिखने से पहले production code के बड़े हिस्से लिखना, TDD guidelines का उल्लंघन करते हुए।
  2. Refactor Phase को skip करना, जो समय के साथ गंदे और maintain करने में मुश्किल codebases की ओर ले जाता है।
  3. ऐसे 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 हैं।

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.