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?
bean
andvalidator
,Order
? – Keepbean#methodName
in the production code.) Most likely, the exception isn't thrown from the try-block shown above. You should be able to verify this in the debugger. – Cartouche