I can't get this to work even for the simplest case.
In my class
public static String fail(somestring) {
throw new RuntimeException()
}
This is my test:
@Test
public void "Throws exception"() throws Exception {
given:
MyClass test = new MyClass()
when:
test.fail("sdlkfjlsdkfj")
then:
thrown(RuntimeException)
// also tried this
//throw new RuntimeException()
}
My it just throws an accept and the test fails. It seems like thrown is just ignored.
My it just throws an accept and the test fails.
I cannot reproduce that.
See the test-question
branch at https://github.com/jeffbrown/red888/tree/351c1a24fa06216c59b858a43562c56cc1dcceb2.
app/src/main/groovy/red888/MyClass.groovy
package red888
class MyClass {
// unclear why this is static in the question, but
// left static here for consistency
public static String fail(somestring) {
throw new RuntimeException()
}
}
app/src/test/groovy/red888/MyClassTest.groovy
package red888
import spock.lang.Specification
class MyClassTest extends Specification {
public void "Throws exception"() throws Exception {
given:
MyClass test = new MyClass()
when:
test.fail("sdlkfjlsdkfj")
then:
thrown(RuntimeException)
}
}
That test passes.
~ $ mkdir demo
~ $ cd demo
demo $ git clone git@github.com:jeffbrown/red888.git
Cloning into 'red888'...
(...)
demo $ cd red888
red888 (main)$ git checkout test-question
Branch 'test-question' set up to track remote branch 'test-question' from 'origin'.
Switched to a new branch 'test-question'
red888 (test-question)$ cat app/src/test/groovy/red888/MyClassTest.groovy
(...)
class MyClassTest extends Specification {
public void "Throws exception"() throws Exception {
(...)
red888 (test-question)$ ./gradlew test
BUILD SUCCESSFUL in 4s
3 actionable tasks: 3 executed