Skip to content

Commit

Permalink
GRAILS-10933 - Add shouldFail helper methods for convenience when mig…
Browse files Browse the repository at this point in the history
…rating away from GroovyTestCase
  • Loading branch information
longwa committed Jan 15, 2014
1 parent df9d405 commit 0d2fa1d
Showing 1 changed file with 58 additions and 1 deletion.
Expand Up @@ -17,10 +17,11 @@
package grails.test.mixin.integration

import groovy.transform.CompileStatic
import junit.framework.AssertionFailedError
import org.codehaus.groovy.runtime.ScriptBytecodeAdapter
import org.junit.Before
import org.codehaus.groovy.grails.test.support.GrailsTestInterceptor
import grails.test.mixin.TestMixinTargetAware
import grails.util.Holders
import org.codehaus.groovy.grails.test.support.GrailsTestMode
import org.junit.After
import groovy.transform.TypeCheckingMode
Expand Down Expand Up @@ -61,4 +62,60 @@ class IntegrationTestMixin implements TestMixinTargetAware {
void destoryIntegrationTest() {
interceptor?.destroy()
}

/**
* Asserts that the given code closure fails when it is evaluated
*
* @param code
* @return the message of the thrown Throwable
*/
String shouldFail(Closure code) {
boolean failed = false
String result = null
try {
code.call()
}
catch (GroovyRuntimeException gre) {
failed = true
result = ScriptBytecodeAdapter.unwrap(gre).getMessage()
}
catch (Throwable e) {
failed = true
result = e.getMessage()
}
if (!failed) {
throw new AssertionFailedError("Closure " + code + " should have failed")
}

return result
}

/**
* Asserts that the given code closure fails when it is evaluated
* and that a particular exception is thrown.
*
* @param clazz the class of the expected exception
* @param code the closure that should fail
* @return the message of the expected Throwable
*/
String shouldFail(Class clazz, Closure code) {
Throwable th = null
try {
code.call()
} catch (GroovyRuntimeException gre) {
th = ScriptBytecodeAdapter.unwrap(gre)
} catch (Throwable e) {
th = e
}

if (th == null) {
throw new AssertionFailedError("Closure $code should have failed with an exception of type $clazz.name")
}

if (!clazz.isInstance(th)) {
throw new AssertionFailedError("Closure $code should have failed with an exception of type $clazz.name, instead got Exception $th")
}

return th.message
}
}

0 comments on commit 0d2fa1d

Please sign in to comment.