कोटलिन में टेस्ट अपवाद अपवाद


94

जावा में, प्रोग्रामर JUnit परीक्षण मामलों के लिए अपेक्षित अपवादों को इस तरह निर्दिष्ट कर सकता है:

@Test(expected = ArithmeticException.class)
public void omg()
{
    int blackHole = 1 / 0;
}

मैं कोटलिन में यह कैसे करूंगा? मैंने दो सिंटैक्स भिन्नताएँ आज़माई हैं, लेकिन उनमें से कोई भी काम नहीं किया है:

import org.junit.Test

// ...

@Test(expected = ArithmeticException) fun omg()
    Please specify constructor invocation;
    classifier 'ArithmeticException' does not have a companion object

@Test(expected = ArithmeticException.class) fun omg()
                            name expected ^
                                            ^ expected ')'

जवाबों:


129

JUnit 4.12 के लिए जावा उदाहरण का कोटलिन अनुवाद है:

@Test(expected = ArithmeticException::class)
fun omg() {
    val blackHole = 1 / 0
}

हालाँकि, JUnit 4.13 नेassertThrows बारीक-बारीक अपवाद स्कोप के लिए दो तरीके पेश किए :

@Test
fun omg() {
    // ...
    assertThrows(ArithmeticException::class.java) {
        val blackHole = 1 / 0
    }
    // ...
}

दोनों assertThrowsतरीके अतिरिक्त अभिकथन के लिए अपेक्षित अपवाद लौटाते हैं:

@Test
fun omg() {
    // ...
    val exception = assertThrows(ArithmeticException::class.java) {
        val blackHole = 1 / 0
    }
    assertEquals("/ by zero", exception.message)
    // ...
}

83

कोटलिन का अपना टेस्ट हेल्पर पैकेज है जो इस तरह की अनइस्टस्ट को करने में मदद कर सकता है।

आपका परीक्षण उपयोग द्वारा बहुत अभिव्यंजक हो सकता है assertFailWith:

@Test
fun test_arithmethic() {
    assertFailsWith<ArithmeticException> {
        omg()
    }
}

1
यदि आपके लिंक पर एक 404 मिलता है, तो उसे kotlin.testकिसी और चीज़ से बदल दिया गया है?
फ्रेडोवरफ्लो

@fredoverflow नहीं, प्रतिस्थापित नहीं किया गया है, लेकिन सिर्फ मानक पुस्तकालयों से हटा दिया गया है। मैंने गथुब कोटलिन रिपॉजिटरी के लिए लिंक अपडेट किया है लेकिन दुर्भाग्य से मुझे प्रलेखन के लिए कोई लिंक नहीं मिला। वैसे भी जार इंटेलीज में कोटलिन-प्लगइन द्वारा शिप किया जाता है या आप इसे नेट पर पा सकते हैं या अपने प्रोजेक्ट में मावेन / ग्रैंडल निर्भरता जोड़ सकते हैं।
मिशेल

7
संकलन "org.jetbrains.kotlin: kotlin-test: $ kotlin_version"
mac229

4
@ mac229 / संकलन / परीक्षण / परीक्षण /
लारेंस गोंसाल्वेस

@ आशीषशर्मा: kotlinlang.org/api/latest/kotlin.test/kotlin.test/… assertFailWith अपवाद को वापस करें और आप इसका उपयोग अपने स्वयं के मुखर को लिखने के लिए कर सकते हैं।
मिशेल डीमिको

26

आप उपयोग कर सकते हैं @Test(expected = ArithmeticException::class)या यहां तक ​​कि कोटलिन के पुस्तकालय के तरीकों में से एक का भी बेहतर उपयोग कर सकते हैं failsWith()

आप इस तरह से संशोधित जेनरिक और सहायक विधि का उपयोग करके इसे और भी छोटा बना सकते हैं:

inline fun <reified T : Throwable> failsWithX(noinline block: () -> Any) {
    kotlin.test.failsWith(javaClass<T>(), block)
}

और उदाहरण एनोटेशन का उपयोग करते हुए:

@Test(expected = ArithmeticException::class)
fun omg() {

}

javaClass<T>()अब पदावनत कर दिया गया है। MyException::class.javaइसके बजाय उपयोग करें ।
फास्ट

विफल होने पर पदावनत किया जाता है, इसके बजाय assertFailsWith का उपयोग किया जाना चाहिए।
ग्वालासोव

15

इसके लिए आप कोटलिनटेस्ट का इस्तेमाल कर सकते हैं ।

अपने परीक्षण में, आप एक ब्लॉक के साथ मध्यस्थ कोड लपेट सकते हैं:

shouldThrow<ArithmeticException> {
  // code in here that you expect to throw a ArithmeticException
}

लगता है लाइन यह उचित तरीके से काम नहीं करता है। मैं चेक करता हूं 1. शोथ्रो <java.lang.AssertionError> {someMethod () .OK shouldBe true} - ग्रीन 2. shouldThrow <java.lang.AssertionError ({someMethod () .OK shouldBe false} - ग्रीन someMethod () थ्रो "जावा"। .lang.AssertionError: संदेश "जब यह होना चाहिए, और ऑब्जेक्ट को वापस करें यदि ठीक है। दोनों मामलों में जब यह ठीक है और जब नहीं है तो हरे रंग का होना चाहिए।
इवान ट्रेचियोकास

शायद डॉक्स पर एक नज़र डालें, यह 2016 में मेरे जवाब के बाद से बदल गया हो सकता है। github.com/kotlintest/kotlintest/blob/master/doc/…
sksamuel

13

JUnit5 में निर्मित कोटलिन समर्थन है।

import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows

class MyTests {
    @Test
    fun `division by zero -- should throw ArithmeticException`() {
        assertThrows<ArithmeticException> {  1 / 0 }
    }
}

3
यह मेरा पसंदीदा जवाब है। यदि आप Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6मुखर हो जाते हैं, तो सुनिश्चित करें कि आपके build.gradle मेंcompileTestKotlin { kotlinOptions.jvmTarget = "1.8" }
बिग कद्दू

11

आप kotlin.test पैकेज के साथ जेनरिक का भी उपयोग कर सकते हैं:

import kotlin.test.assertFailsWith 

@Test
fun testFunction() {
    assertFailsWith<MyException> {
         // The code that will throw MyException
    }
}

1

एसेट एक्सटेंशन जो अपवाद वर्ग की पुष्टि करता है और त्रुटि संदेश से मेल खाने पर भी।

inline fun <reified T : Exception> assertThrows(runnable: () -> Any?, message: String?) {
try {
    runnable.invoke()
} catch (e: Throwable) {
    if (e is T) {
        message?.let {
            Assert.assertEquals(it, "${e.message}")
        }
        return
    }
    Assert.fail("expected ${T::class.qualifiedName} but caught " +
            "${e::class.qualifiedName} instead")
}
Assert.fail("expected ${T::class.qualifiedName}")

}

उदाहरण के लिए:

assertThrows<IllegalStateException>({
        throw IllegalStateException("fake error message")
    }, "fake error message")

1

किसी ने उल्लेख नहीं किया कि assertFailsWith () मान लौटाता है और आप अपवाद विशेषताओं की जांच कर सकते हैं:

@Test
fun `my test`() {
        val exception = assertFailsWith<MyException> {method()}
        assertThat(exception.message, equalTo("oops!"))
    }
}

0

क्लूंट का उपयोग करके सिंटैक्सिस का एक और संस्करण :

@Test
fun `should throw ArithmeticException`() {
    invoking {
        val backHole = 1 / 0
    } `should throw` ArithmeticException::class
}

0

(expected = YourException::class)परीक्षण एनोटेशन में जोड़ने के लिए फ़र्ट स्टेप्स हैं

@Test(expected = YourException::class)

दूसरा चरण इस फ़ंक्शन को जोड़ना है

private fun throwException(): Boolean = throw YourException()

अंत में आपके पास कुछ इस तरह होगा:

@Test(expected = ArithmeticException::class)
fun `get query error from assets`() {
    //Given
    val error = "ArithmeticException"

    //When
    throwException()
    val result =  omg()

    //Then
    Assert.assertEquals(result, error)
}
private fun throwException(): Boolean = throw ArithmeticException()

0

org.junit.jupiter.api.Assertions.kt

/**
 * Example usage:
 * ```kotlin
 * val exception = assertThrows<IllegalArgumentException>("Should throw an Exception") {
 *     throw IllegalArgumentException("Talk to a duck")
 * }
 * assertEquals("Talk to a duck", exception.message)
 * ```
 * @see Assertions.assertThrows
 */
inline fun <reified T : Throwable> assertThrows(message: String, noinline executable: () -> Unit): T =
        assertThrows({ message }, executable)
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.