How Java exceptions are handled in Spock Framework? It is a fifteenth question in the quiz for a full-stack developer.
There is a class written in Java with a method that throws NullPointerException.
public class MyClass {
public void myMethod() {
throw new NullPointerException();
}
}
This is a test class in Spock:
class MyClassSpec extends Specification {
def "test myMethod"() {
given:
MyClass myClass = new MyClass()
when:
myClass.myMethod()
then:
<assertion>
}
}
Which of the following assertion can be put in the <assertion> place to make the test pass? Choose all correct answers.
- thrown(Exception)
- thrown(NullPointerException)
- assert Exception
- thrown(ParseException)
- notThrown(Exception)
- thrown()
For the answer scroll down
.
.
.
.
.
.
The correct answers are a, b. For more information about it, read Testing for exceptions in Spock.