How do we test the exception?



  • There's a function of DoIt, which is the test (JUnit). If the argument is null, the function must remove the exception. I'm checking this:

    public final void TestDoIt() {
        try {
            // Вызываю функцию
            DoIt(null);
            fail("doit(null) должна выкинуть исключение");
        } catch (NullPointerException e) {
        }
    }
    

    Is that right or is there something better?



  • That's right for JUnit until version 4.

    New versions can (and need) use annotations:

     /users/1155/test (expected = NullPointerException.class)
    public final void doIt() {
        DoIt(null);
    }
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2