2

Hey All I have a question that I try to resolve. I am using selenium with pure java and test NG. In the test class I want to call a method and if the method throw exception I want to fail the test. this is the method:

public  void insertAddress2(String address2) throws Exception {}

and i want to call it from a test class, and if it (the insert address2 method) throw exception I want the test to fail.

this is the the class test Assertion statement

@Test(groups = {"address"},enabled = true)
public void testAddress() throws Exception {
Assert.assertNotEquals(adderess.insertAddress2("test"),ErrorMessagePermission, "");
}

can someone please advise what is the syntax and how to test not exception thrown in the insertAddress2 method from different class

Bastian
  • 1,089
  • 7
  • 25
  • 74
  • 1
    Does this answer your question? [How to test that no exception is thrown?](https://stackoverflow.com/questions/17731234/how-to-test-that-no-exception-is-thrown) – Amongalen Mar 24 '20 at 09:06
  • I believe the same applies to testNG and selenium – Amongalen Mar 24 '20 at 09:06

1 Answers1

1

Basically, even in tests, when you want to react on exceptions, you would have to catch them:

@Test(groups = {"address"}, enabled = true)
public void testAddress() { //no throws decleration required
    try {
        adderess.insertAddress2("test");
    } catch (Exception ex) {
        Assert.fail("Exception was thrown!");
    }
}
Martin Ackermann
  • 884
  • 6
  • 15