Skip to content

Commit fc7d416

Browse files
committed
Merge pull request #456 from longwa/GRAILS-10933
GRAILS-10933 - Add shouldFail helper methods for convenience when migrat...
2 parents 6367b86 + 0d2fa1d commit fc7d416

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

grails-plugin-testing/src/main/groovy/grails/test/mixin/integration/IntegrationTestMixin.groovy

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
package grails.test.mixin.integration
1818

1919
import groovy.transform.CompileStatic
20+
import junit.framework.AssertionFailedError
21+
import org.codehaus.groovy.runtime.ScriptBytecodeAdapter
2022
import org.junit.Before
2123
import org.codehaus.groovy.grails.test.support.GrailsTestInterceptor
2224
import grails.test.mixin.TestMixinTargetAware
23-
import grails.util.Holders
2425
import org.codehaus.groovy.grails.test.support.GrailsTestMode
2526
import org.junit.After
2627
import groovy.transform.TypeCheckingMode
@@ -61,4 +62,60 @@ class IntegrationTestMixin implements TestMixinTargetAware {
6162
void destoryIntegrationTest() {
6263
interceptor?.destroy()
6364
}
65+
66+
/**
67+
* Asserts that the given code closure fails when it is evaluated
68+
*
69+
* @param code
70+
* @return the message of the thrown Throwable
71+
*/
72+
String shouldFail(Closure code) {
73+
boolean failed = false
74+
String result = null
75+
try {
76+
code.call()
77+
}
78+
catch (GroovyRuntimeException gre) {
79+
failed = true
80+
result = ScriptBytecodeAdapter.unwrap(gre).getMessage()
81+
}
82+
catch (Throwable e) {
83+
failed = true
84+
result = e.getMessage()
85+
}
86+
if (!failed) {
87+
throw new AssertionFailedError("Closure " + code + " should have failed")
88+
}
89+
90+
return result
91+
}
92+
93+
/**
94+
* Asserts that the given code closure fails when it is evaluated
95+
* and that a particular exception is thrown.
96+
*
97+
* @param clazz the class of the expected exception
98+
* @param code the closure that should fail
99+
* @return the message of the expected Throwable
100+
*/
101+
String shouldFail(Class clazz, Closure code) {
102+
Throwable th = null
103+
try {
104+
code.call()
105+
} catch (GroovyRuntimeException gre) {
106+
th = ScriptBytecodeAdapter.unwrap(gre)
107+
} catch (Throwable e) {
108+
th = e
109+
}
110+
111+
if (th == null) {
112+
throw new AssertionFailedError("Closure $code should have failed with an exception of type $clazz.name")
113+
}
114+
115+
if (!clazz.isInstance(th)) {
116+
throw new AssertionFailedError("Closure $code should have failed with an exception of type $clazz.name, instead got Exception $th")
117+
}
118+
119+
return th.message
120+
}
64121
}

0 commit comments

Comments
 (0)