Full-stack dev quiz question #15

Full-stack dev quiz question #15

quizHow 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.

  1. thrown(Exception)
  2. thrown(NullPointerException)
  3. assert Exception
  4. thrown(ParseException)
  5. notThrown(Exception)
  6. thrown()

 

For the answer scroll down

.

.

.

 

 

 

 

 

 

 

 

 

 

 

.

.

.

The correct answers are a, b. For more information about it, read Testing for exceptions in Spock.