Will Will - 6 months ago 20
Java Question

Custom @Annotation to report boolean at runtime

I'm searching around to see if what I want to accomplish is possible with annotations.

Basically, we have a bunch of TestNG test cases which we're micro managing

Example:

@Test
public void reportingTest(){
Assert.true(false);
}


The above would simply fail, but we wrap everything in an assertion try catch.

@Test
public void reportingTest(){
try {
Assert.true(false);
Report.batch(Enum.Pass, 106);
} catch (Throwable t) {
Report.batch(Enum.Fail, 106, "Test case has failed");
}
}


However, after hundreds of test-cases... having that try catch is super cumbersome.

I am trying to accomplish something like this

@Reporting(id=106)
@Test
public void reportingTest(){
Assert.true(false);
}


Inside of the annotation I would have the ability to capture the failed assertion and send a log off based on my id.

Thanks!

Answer

TestNG provides listeners and the one you are looking for may be the TestListener.

Your annotation will be available from there: tr.getMethod().getConstructorOrMethod().getMethod().getAnnotation(Reporting.class).

Comments