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.

We use cookies

We use cookies on our website. Some of them are essential for the operation of the site, while others help us to improve this site and the user experience (tracking cookies). You can decide for yourself whether you want to allow cookies or not. Please note that if you reject them, you may not be able to use all the functionalities of the site.