Spock throw exception test
groovy, java, spock, unit-testing
Solution
I believe your `then` block needs to be fixed. Try the following syntax:
then:
thrown CustomException
Problem
I test Java code with Spock. I test this code: ``` try { Set<String> availableActions = getSthAction() List<String> goodActions = getGoodAction() if (!CollectionUtils.containsAny(availableActions ,goodActions )){ throw new CustomException(); } } catch (AnotherCustomExceptio e) { throw new CustomException(e.getMessage()); } ``` I wrote test: ``` def "some test"() { given: bean.methodName(_) >> {throw new AnotherCustomExceptio ("Sth wrong")} def order = new Order(); when: validator.validate(order ) then: final CustomException exception = thrown() } ``` And it fails because `AnotherCustomExceptio` is thrown. But in the `try{}catch` block I catch this exception and throw a `CustomException` so I expected that my method will throw `CustomException` and not `AnotherCustomExceptio`. How do I test it?