I'm not sure if this is a bug in TestNG or not. I believe it is. I am applying the same idea as described in that article. Here is what I have in my test class.
@Test
public void testMissingFields() {
logFailMessage("1");
logFailMessage("2");
}
@TestAssertion
@Test
public void logTestResult() {
if (!StringUtil.isEmpty(getFailMessage())) {
Assert.fail(getFailMessage());
}
Here is my IMethodInteceptor implementation.
public class TestInterceptor implements IMethodInterceptor {
@Override
public List<IMethodInstance> intercept(List<IMethodInstance> methods,
ITestContext context) {
List<IMethodInstance> result = new ArrayList<IMethodInstance>();
IMethodInstance assertion = null;
// find the assertion method
for (IMethodInstance method : methods) {
//if (method.getMethod().getConstructorOrMethod().getMethod().isAnnotationPresent(TestAssertion.class)) {
Method m = method.getMethod().getConstructorOrMethod().getMethod();
Annotation[] annotations = m.getAnnotations();
if (m.isAnnotationPresent(TestAssertion.class)) {
//if (method.getMethod().getMethod().isAnnotationPresent(TestAssertion.class)) {
assertion = method;
break;
}
}
if (assertion == null) {
return methods;
}
for (IMethodInstance method : methods) {
if (method != assertion) {
result.add(method);
result.add(assertion); // so it runs after the @Test method
}
}
return result;
}
}
When I debug my test class, the method inteceptor can only find 1 annotation on each test method and that is @Test. It did not find the other annotations such as the @TestAssertion that I have.
I think I'm doing this right from what I understand.