Skip to content

Commit 3e98346

Browse files
m-koopsTimvdLippe
authored andcommitted
Clean up tests asserting failure states: introduce an 'assertThrows()' assertion inspired by a similar assertion in JUnit5. The new assertion function asserts an exception of the given type is thrown by the block lambda and the exception is returned for further assertions.
1 parent 8d54856 commit 3e98346

File tree

3 files changed

+30
-36
lines changed

3 files changed

+30
-36
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package test
2+
3+
import junit.framework.AssertionFailedError
4+
5+
/** Inspired by JUnit5, asserts an exception being thrown */
6+
inline fun <reified E: Throwable> assertThrows(block: () -> Unit): E {
7+
return try {
8+
block.invoke()
9+
throw AssertionFailedError("No exception of type ${(E::class).simpleName} was thrown")
10+
} catch (e: Throwable) {
11+
e as? E ?: throw e
12+
}
13+
}

tests/src/test/kotlin/test/MockingTest.kt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -180,16 +180,14 @@ class MockingTest : TestBase() {
180180
System.setOut(out)
181181
val mock = mock<SynchronousFunctions>(verboseLogging = true)
182182

183-
try {
184-
/* When */
183+
184+
/* When, Then */
185+
assertThrows<WantedButNotInvoked> {
185186
verify(mock).stringResult()
186-
fail("Expected an exception")
187-
} catch (_: WantedButNotInvoked) {
188-
/* Then */
189-
argumentCaptor<DescribedInvocation>().apply {
190-
verify(out).println(capture())
191-
expect(lastValue.toString()).toBe("synchronousFunctions.stringResult();")
192-
}
187+
}
188+
argumentCaptor<DescribedInvocation>().apply {
189+
verify(out).println(capture())
190+
expect(lastValue.toString()).toBe("synchronousFunctions.stringResult();")
193191
}
194192
}
195193

tests/src/test/kotlin/test/OngoingStubbingTest.kt

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package test
22

33
import com.nhaarman.expect.expect
44
import com.nhaarman.expect.expectErrorWithMessage
5-
import com.nhaarman.expect.fail
65
import org.junit.Assume.assumeFalse
76
import org.junit.Test
87
import org.mockito.Mockito
@@ -64,11 +63,9 @@ class OngoingStubbingTest : TestBase() {
6463
on { builderMethod() } doThrow IllegalArgumentException()
6564
}
6665

67-
try {
68-
/* When */
66+
/* When, Then */
67+
assertThrows<IllegalArgumentException> {
6968
mock.builderMethod()
70-
fail("No exception thrown")
71-
} catch (_: IllegalArgumentException) {
7269
}
7370
}
7471

@@ -79,11 +76,9 @@ class OngoingStubbingTest : TestBase() {
7976
on { builderMethod() } doThrow IllegalArgumentException::class
8077
}
8178

82-
try {
83-
/* When */
79+
/* When, Then */
80+
assertThrows<IllegalArgumentException> {
8481
mock.builderMethod()
85-
fail("No exception thrown")
86-
} catch (_: IllegalArgumentException) {
8782
}
8883
}
8984

@@ -97,18 +92,12 @@ class OngoingStubbingTest : TestBase() {
9792
)
9893
}
9994

100-
try {
101-
/* When */
95+
/* When, Then */
96+
assertThrows<IllegalArgumentException> {
10297
mock.builderMethod()
103-
fail("No exception thrown")
104-
} catch (_: IllegalArgumentException) {
10598
}
106-
107-
try {
108-
/* When */
99+
assertThrows<UnsupportedOperationException> {
109100
mock.builderMethod()
110-
fail("No exception thrown")
111-
} catch (_: UnsupportedOperationException) {
112101
}
113102
}
114103

@@ -122,18 +111,12 @@ class OngoingStubbingTest : TestBase() {
122111
)
123112
}
124113

125-
try {
126-
/* When */
114+
/* When, Then */
115+
assertThrows<IllegalArgumentException> {
127116
mock.builderMethod()
128-
fail("No exception thrown")
129-
} catch (_: IllegalArgumentException) {
130117
}
131-
132-
try {
133-
/* When */
118+
assertThrows<UnsupportedOperationException> {
134119
mock.builderMethod()
135-
fail("No exception thrown")
136-
} catch (_: UnsupportedOperationException) {
137120
}
138121
}
139122

0 commit comments

Comments
 (0)