← Back to Kotlin Course | Chapter 13: Error Handling & Testing | Lesson 4 of 6

JUnit के Basics

JUnit एक लोकप्रिय helper toolkit है जो आपको छोटे automated checks लिखने देता है जो पुष्टि करते हैं कि आपका Kotlin code उम्मीद के मुताबिक व्यवहार करता है।
Syntax
kotlin
class TestClassName {
    @BeforeEach
    fun setUp() { /* prepare */ }

    @Test
    fun testName() {
        assertEquals(expected, actual)
    }
}

Writing a Basic JUnit Test

एक JUnit test @Test से annotated एक function है जो यह जांचने के लिए कि असली behavior एक expected value से मेल खाता है एक assertion function, जैसे assertEquals, call करता है।

Note: Backtick-quoted function names, जैसे ` adding two numbers returns their sum() `, descriptive JUnit test names के लिए एक आम Kotlin convention है।
Warning: यह उदाहरण एक third-party testing/mocking library उपयोग करता है जो साधारण Kotlin standard library का हिस्सा नहीं है और इसे एक project dependency के रूप में जोड़ना चाहिए (जैसे Gradle के ज़रिए) -- यह बिना किसी dependency वाले plain kotlinc sandbox में नहीं चल सकता।

उदाहरण: Writing a Basic JUnit Test

markup
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Assertions.assertEquals

class CalculatorTest {
    @Test
    fun `adding two numbers returns their sum`() {
        val result = 2 + 3
        assertEquals(5, result)
    }
}

Common Assertions

JUnit assertEquals, assertTrue, assertFalse, और assertThrows जैसे assertion functions प्रदान करता है, हर एक उम्मीद पूरी न होने पर test को एक साफ़ message के साथ fail करता है।

Warning: यह उदाहरण एक third-party testing/mocking library उपयोग करता है जो साधारण Kotlin standard library का हिस्सा नहीं है और इसे एक project dependency के रूप में जोड़ना चाहिए (जैसे Gradle के ज़रिए) -- यह बिना किसी dependency वाले plain kotlinc sandbox में नहीं चल सकता।

उदाहरण: Common Assertions

markup
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Assertions.*

class ValidationTest {
    @Test
    fun `age validation rejects negative numbers`() {
        assertThrows(IllegalArgumentException::class.java) {
            require(-1 >= 0) { "Age cannot be negative" }
        }
    }
}

Organizing Tests with setUp

@BeforeEach से annotated एक method किसी class के हर test से पहले चलता है, shared fixtures setup करने के लिए उपयोगी ताकि हर test एक ज्ञात, साफ़ state से शुरू हो।

Warning: यह उदाहरण एक third-party testing/mocking library उपयोग करता है जो साधारण Kotlin standard library का हिस्सा नहीं है और इसे एक project dependency के रूप में जोड़ना चाहिए (जैसे Gradle के ज़रिए) -- यह बिना किसी dependency वाले plain kotlinc sandbox में नहीं चल सकता।

उदाहरण: Organizing Tests with setUp

markup
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Assertions.assertEquals

class StackTest {
    private lateinit var stack: MutableList<Int>

    @BeforeEach
    fun setUp() {
        stack = mutableListOf()
    }

    @Test
    fun `pushing adds an element`() {
        stack.add(1)
        assertEquals(1, stack.size)
    }
}

Running Tests with Gradle

किसी Gradle-based Kotlin project में, JUnit tests ./gradlew test से execute किए जाते हैं, जो test sources को compile करता है और report करता है कि कौन से tests pass या fail हुए।

उदाहरण: Running Tests with Gradle

bash
./gradlew test

⚠️ Run this in your own terminal or Node.js environment.

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. यह भूल जाना कि JUnit एक third-party test framework है जिसे एक build tool (Gradle/Maven) dependency चाहिए; यह सिर्फ kotlinc के साथ किसी साधारण .kt file से नहीं चल सकता।
  2. ऐसे test methods लिखना जो किसी साफ़, descriptive नाम से शुरू न हों, test runner के output से अकेले failures समझना मुश्किल बनाते हुए।
  3. अलग, केंद्रित tests में बांटने के बजाय एक single test function में कई असंबंधित behaviors test करना।
चैप्टर सारांश
  • JUnit Kotlin सहित JVM languages के लिए एक व्यापक रूप से उपयोग होने वाला testing framework है, और उपयोग करने के लिए एक Gradle/Maven dependency चाहिए।
  • एक test function @Test से चिह्नित होता है और आमतौर पर assertEquals, assertTrue, या similar assertion functions उपयोग करता है।
  • Test classes और functions आमतौर पर किसी project की src/test/kotlin directory के तहत रखे जाते हैं, main source code से अलग।
  • ./gradlew test चलाना किसी Gradle-based Kotlin project में सभी JUnit tests execute करता है।
🔒

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.