[Java] Force an error [Exception] in Spock

less than 1 minute read

ServiceImpl


     def "abnormal" () {
         setup:
// service.createBody(_ as boolean) >> {throw new RuntimeException("dummy")}
         service.createBody(_) >> {throw new RuntimeException("dummy")}

        
         when:
         String testString = service.send(to, hash)

         then:
         RuntimeException e = thrown()
         e.getMessage() == expected

         where:
         testcase | to | hash | expected
         "test" | "[email protected]" | "testhash" | "dummy"

     }

In IntelliJ, the _ part of service.createBody(_) turned yellow and I always complemented it. If it is just _, an Exception will occur. Is it special when it is boolean? It may be useful to remember when you want to force an Exception.