Annotation testNG which will be "finally" in java?

I don't know if my question was clear, but I am using testNG and I have this:

@Test
public void passengerServiceTest() {
...
}

@AfterTest
public void deleteCreatedPassenger() {
...    
}

      

I want to execute my deleteCreatedPassenger () method after receiving the sensorServiceTest package, also if I want the passenger service function to fail if deleteCreatedPassenger fails, in other words, I want both of them to be the same, so if one of them is not works, test fails. So I tried with annotations @AfterTest, @AfterMethod, @AfterClass and all to make the two tests as "split" tests. Do you know how to do it? Relations

+3


source to share


1 answer


You don't need annotations to achieve this, as this is exactly what the finally block is for:

@Test
public void passengerServiceTest() {            
    try {
        //test code 
    } finally {
        deleteCreatedPassenger();
    }
}

public void deleteCreatedPassenger() {
...    
}

      



If the exception throws an exception, your test test fails.

Annotations are useful in certain scenarios; you shouldn't aim to use them in your host language constructs!

+3


source







All Articles