0

ATest.java - Can we verify, when no exception occurred in a testdisplay2() method like we use expected when some exception occurred in testdisplay1()

class A{
  public void display(){
    ...
    ...
    ...
    if(a){
     throw new AIsTrueException("a true");
    }

    if(b){
     throw new BIsTrueException("b true");
    }

  }
}

class BTest{
...
@InjectMock
A subject;
...
@Test(expected=AIsTrueException.class)
public void testdisplay1(){
    subject.display(true,false);
}

@Test
public void testdisplay2(){
    subject.display(false,false);
    // Here how can I verify that no exception has occured in display(),
    // (When exception occured we use expected = ...)
    // Do we have similar when no exception occured
} 
...
...
}

2 Answers2

1

What you have is correct. Try changing display() to always throw an AIsTrueException and you will see that testdisplay2() gives an error.

Chrs
  • 862
  • 6
  • 10
1

The expected behaviour is that no exception will be thrown. If any is thrown the test will fail unless there is an expected parameter in the annotation like the one you have in the testdisplay1 test.

Also, this is a nice read: How to test that no exception is thrown?

Community
  • 1
  • 1
Marc
  • 356
  • 1
  • 8