Bastian
Bastian

Reputation: 1237

How to Assert No exception thrown TestNG java

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 ?

Upvotes: 4

Views: 2858

Answers (1)

Martin Ackermann
Martin Ackermann

Reputation: 924

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!");
    }
}

Upvotes: 2

Related Questions