Skip to content

Commit 141a784

Browse files
committed
Use function for expected failures
1 parent 78f75ab commit 141a784

File tree

16 files changed

+116
-84
lines changed

16 files changed

+116
-84
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ If you are going to write code, choose the appropriate base branch:
210210

211211
There are a number of tests for functionality that is broken, mostly in the [failing](https://github.com/FasterXML/jackson-module-kotlin/tree/master/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/failing)
212212
package but a few as part of other test suites. Instead of ignoring these tests (with JUnit's `@Ignore` annotation)
213-
or excluding them from being run as part of automated testing, the tests are written so as to demonstrate the failure
213+
or excluding them from being run as part of automated testing, the tests are written to demonstrate the failure
214214
(either making a call that throws an exception or with an assertion that fails) but not fail the build, except if the
215215
underlying issue is fixed. This allows us to know when the tested functionality has been incidentally fixed by
216216
unrelated code changes.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.fasterxml.jackson.module.kotlin.test
2+
3+
import kotlin.reflect.KClass
4+
import kotlin.test.fail
5+
6+
/**
7+
* Expect a block to throw an exception. If a different type of exception is thrown or no
8+
* exception is produced by the block, fail the test. In the latter case, no exception being
9+
* thrown, fixMessage is printed.
10+
*
11+
* This function is intended to allow failing tests to be written and run as part of the build
12+
* without causing it to fail, except if the failure is fixed, in which case the fixMessage
13+
* should make it clear what has happened (i.e. that some previously broken functionality
14+
* has been fixed).
15+
*
16+
* @param throwableType The expected throwable
17+
* @param fixMessage The message to print when the block succeeds
18+
* @param block The block to execute
19+
*/
20+
fun <T : Throwable> expectFailure(
21+
throwableType: KClass<T>,
22+
fixMessage: String,
23+
block: () -> Unit
24+
) {
25+
try {
26+
block()
27+
} catch (e: Throwable) {
28+
if (throwableType.isInstance(e)) {
29+
// Expected exception, do nothing
30+
} else {
31+
fail("Expected $throwableType but got $e")
32+
}
33+
34+
return
35+
}
36+
37+
fail(fixMessage)
38+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fasterxml.jackson.module.kotlin.test
2+
3+
import org.junit.Assert.assertThrows
4+
import org.junit.Test
5+
import kotlin.test.assertEquals
6+
7+
class TestHelpersTest {
8+
@Test
9+
fun expectFailure_ExpectedExceptionThrown() {
10+
expectFailure(AssertionError::class, "This will not be printed") {
11+
throw AssertionError("This is expected")
12+
}
13+
}
14+
15+
@Test
16+
fun expectFailure_AnotherExceptionThrown() {
17+
val throwable = assertThrows(AssertionError::class.java) {
18+
expectFailure(AssertionError::class, "This will not be printed") {
19+
throw Exception("This is not expected")
20+
}
21+
}
22+
23+
assertEquals("Expected class java.lang.AssertionError but got java.lang.Exception: This is not expected", throwable.message)
24+
}
25+
26+
@Test
27+
fun expectFailure_NoExceptionThrown() {
28+
val fixMessage = "Test will fail with this message"
29+
val throwable = assertThrows(AssertionError::class.java) {
30+
expectFailure(AssertionError::class, fixMessage) {
31+
// Do nothing
32+
}
33+
}
34+
35+
assertEquals(fixMessage, throwable.message)
36+
}
37+
}

src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/failing/Github138.kt

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,26 @@ import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement
88
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlText
99
import com.fasterxml.jackson.module.kotlin.readValue
1010
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
11+
import com.fasterxml.jackson.module.kotlin.test.expectFailure
1112
import org.junit.Test
12-
import kotlin.test.fail
1313

1414
class TestGithub138 {
1515
@JacksonXmlRootElement(localName = "sms")
1616
@JsonIgnoreProperties(ignoreUnknown = true)
1717
data class Sms(
18-
@JacksonXmlProperty(localName = "Phone", isAttribute = true)
19-
val phone: String?,
18+
@JacksonXmlProperty(localName = "Phone", isAttribute = true)
19+
val phone: String?,
2020

21-
@JacksonXmlText
22-
val text: String? = ""
23-
24-
)
21+
@JacksonXmlText
22+
val text: String? = ""
23+
)
2524

2625
@Test
2726
fun testDeserProblem() {
2827
val xml = """<sms Phone="435242423412" Id="43234324">Lorem ipsum</sms>"""
2928
val xmlMapper = XmlMapper().registerKotlinModule()
30-
try {
29+
expectFailure(InvalidDefinitionException::class, "GitHub #138 has been fixed!") {
3130
val jsms = xmlMapper.readValue<Sms>(xml)
32-
fail("GitHub #138 has been fixed!")
33-
} catch (e: InvalidDefinitionException) {
34-
// Remove this try/catch and the `fail()` call above when this issue is fixed
3531
}
3632
}
3733
}

src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/failing/Github160DisableAnnotations.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,17 @@ import com.fasterxml.jackson.databind.MapperFeature
44
import com.fasterxml.jackson.databind.exc.MismatchedInputException
55
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
66
import com.fasterxml.jackson.module.kotlin.readValue
7+
import com.fasterxml.jackson.module.kotlin.test.expectFailure
78
import org.junit.Test
8-
import kotlin.test.fail
99

1010
class TestGithub160 {
1111
data class DataClass(val blah: String)
1212

1313
@Test
1414
fun dataClass() {
1515
val mapper = jacksonObjectMapper().configure(MapperFeature.USE_ANNOTATIONS, false)
16-
try {
16+
expectFailure(MismatchedInputException::class, "GitHub #160 has been fixed!") {
1717
mapper.readValue<DataClass>("""{"blah":"blah"}""")
18-
fail("GitHub #160 has been fixed!")
19-
} catch (e: MismatchedInputException) {
20-
// Remove this try/catch and the `fail()` call above when this issue is fixed
2118
}
2219
}
2320
}

src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/failing/Github271AlphaSortProperties.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package com.fasterxml.jackson.module.kotlin.test.failing
22

33
import com.fasterxml.jackson.annotation.JsonPropertyOrder
44
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
5+
import com.fasterxml.jackson.module.kotlin.test.expectFailure
56
import org.junit.Test
67
import kotlin.test.assertEquals
7-
import kotlin.test.fail
88

99
class TestGithub271 {
1010
@JsonPropertyOrder(alphabetic=true)
@@ -17,11 +17,8 @@ class TestGithub271 {
1717
val mapper = jacksonObjectMapper()
1818

1919
val json = mapper.writeValueAsString(Foo("a", "c"))
20-
try {
20+
expectFailure(AssertionError::class, "GitHub #271 has been fixed!") {
2121
assertEquals("""{"a":"a","b":"b","c":"c"}""", json)
22-
fail("GitHub #271 has been fixed!")
23-
} catch (e: AssertionError) {
24-
// Remove this try/catch and the `fail()` call above when this issue is fixed
2522
}
2623
}
2724
}

src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/failing/Github335.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import com.fasterxml.jackson.annotation.JsonTypeInfo.As
77
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id
88
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
99
import com.fasterxml.jackson.module.kotlin.readValue
10+
import com.fasterxml.jackson.module.kotlin.test.expectFailure
1011
import org.junit.Test
1112
import kotlin.test.assertNull
12-
import kotlin.test.fail
1313

1414
class Github335Test {
1515
val mapper = jacksonObjectMapper()
@@ -30,12 +30,9 @@ class Github335Test {
3030
val json = mapper.writeValueAsString(oldEntity)
3131
val newEntity = mapper.readValue<MyEntity>(json)
3232

33-
try {
34-
// newEntity.type is hte string "null" instead of the null value
33+
expectFailure(AssertionError::class, "GitHub #335 has been fixed!") {
34+
// newEntity.type is the string "null" instead of the null value
3535
assertNull(newEntity.type)
36-
fail("GitHub #335 has been fixed!")
37-
} catch (e: AssertionError) {
38-
// Remove this try/catch and the `fail()` call above when this issue is fixed
3936
}
4037
}
4138
}

src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/failing/Github340.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import com.fasterxml.jackson.databind.ObjectMapper
44
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException
55
import com.fasterxml.jackson.module.kotlin.KotlinModule
66
import com.fasterxml.jackson.module.kotlin.readValue
7+
import com.fasterxml.jackson.module.kotlin.test.expectFailure
78
import org.junit.Test
89
import kotlin.test.assertEquals
9-
import kotlin.test.fail
1010

1111
class OwnerRequestTest {
1212
private val jackson = ObjectMapper().registerModule(KotlinModule())
@@ -21,12 +21,9 @@ class OwnerRequestTest {
2121

2222
@Test
2323
fun testDeserHit340() {
24-
try {
24+
expectFailure(UnrecognizedPropertyException::class, "GitHub #340 has been fixed!") {
2525
val value: IsField = jackson.readValue(json)
2626
assertEquals("Got a foo", value.foo)
27-
fail("GitHub #340 has been fixed!")
28-
} catch (e: UnrecognizedPropertyException) {
29-
// Remove this try/catch and the `fail()` call above when this issue is fixed
3027
}
3128
}
3229

src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/failing/Github396.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.fasterxml.jackson.module.kotlin.test.failing
33
import com.fasterxml.jackson.databind.exc.MismatchedInputException
44
import com.fasterxml.jackson.dataformat.xml.XmlMapper
55
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
6+
import com.fasterxml.jackson.module.kotlin.test.expectFailure
67
import kotlin.test.Test
78
import kotlin.test.assertEquals
89
import kotlin.test.fail
@@ -17,13 +18,10 @@ class TestGithub396 {
1718
val mapper = XmlMapper().registerKotlinModule()
1819

1920
val xml = "<product><stuff></stuff></product>"
20-
try {
21+
expectFailure(MismatchedInputException::class, "GitHub #396 has been fixed!") {
2122
val product: Product = mapper.readValue(xml, Product::class.java)
2223

2324
assertEquals(Product(null), product)
24-
fail("GitHub #396 has been fixed!")
25-
} catch (e: MismatchedInputException) {
26-
// Remove this try/catch and the `fail()` call above when this issue is fixed
2725
}
2826
}
2927

src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/failing/Github50.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import com.fasterxml.jackson.annotation.JsonUnwrapped
44
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException
55
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
66
import com.fasterxml.jackson.module.kotlin.readValue
7+
import com.fasterxml.jackson.module.kotlin.test.expectFailure
78
import org.junit.Test
8-
import kotlin.test.fail
99

1010
class TestGithub50 {
1111
data class Name(val firstName: String, val lastName: String)
@@ -18,11 +18,8 @@ class TestGithub50 {
1818
@Test
1919
fun testGithub50UnwrappedError() {
2020
val json = """{"firstName":"John","lastName":"Smith","position":"Manager"}"""
21-
try {
21+
expectFailure(InvalidDefinitionException::class, "GitHub #50 has been fixed!") {
2222
val obj: Employee = jacksonObjectMapper().readValue(json)
23-
fail("GitHub #50 has been fixed!")
24-
} catch (e: InvalidDefinitionException) {
25-
// Remove this try/catch and the `fail()` call above when this issue is fixed
2623
}
2724
}
2825
}

0 commit comments

Comments
 (0)