6

Hey All I have a question that I try to resolve. I am using selenium with pure java and TestNG framework. In the test class I want to call a method and if the method throws exception I want the test to fail.

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 insertAddress2 method) throws 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 in TestNG and how to test exception not thrown in the insertAddress2 method from the test class ?

2
  • 1
    Does this answer your question? How to test that no exception is thrown? Commented Mar 24, 2020 at 9:06
  • I believe the same applies to testNG and selenium Commented Mar 24, 2020 at 9:06

1 Answer 1

2

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!"); } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.