@Test annotation is the best way to achieve this and there are various guides online to do it.
Simply put you'd create a new annotation class
e.g.
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface TestParameters {
String[] testRailId() default "";
}
Then within your test put the test / case ID (without the preceding T or C, as the testrail API doesn't accept them).
@Test(priority = 0, description = "Some description")
@TestParameters(testRailId= {"1234"})
public void My_Test(){
//do something
}
Then in the listener:
String[] testID;
IClass obj = iTestResult.getTestClass();
Class<?> newobj = obj.getRealClass();
Method testMethod = null;
testMethod = newobj.getMethod(iTestResult.getMethod().getMethodName());
if (testMethod.isAnnotationPresent(TestParameters.class)) {
TestParameters useAsTestName = testMethod.getAnnotation(TestParameters.class);
// Get the TestCase ID for Test Rail
testID = (useAsTestName.testRailId());
// Do something with the testID
}
There are other ways to do it, but they usually start to fall over when you're using TestNG's parallel testing abilities.