I’ve written a quick example that shows how to use custom annotations on JUnit tests to map them to Quality Center test cases.
You can create an annotation called “QCTestCases” and add it to your JUnit tests like this:
package oneshore.example.qcintegration;
import static org.junit.Assert.*;
import org.junit.Test;
public class MyTest extends TestBase {
@Test
@QCTestCases(covered={"QC-TEST-1", "QC-TEST-2"}, related={"QC-TEST-3"})
public void testSomething() {
assertTrue(true);
}
}
The annotation is a simple interface:
package oneshore.example.qcintegration;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface QCTestCases {
String[] covered();
String[] related();
}
And the base class uses reflection to discover if the annotation has been added for the test case. You can then use it to report the coverage:
package oneshore.example.qcintegration;
import java.lang.reflect.Method;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.TestName;
public class TestBase {
@Rule public TestName testName = new TestName();
QCTestCases qcTestCases;
@Before
public void getQCTestCoverage() {
try {
Method m = this.getClass().getMethod(testName.getMethodName());
if (m.isAnnotationPresent(QCTestCases.class)) {
qcTestCases = m.getAnnotation(QCTestCases.class);
}
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
@After
public void writeCoverageReport() {
StringBuilder result = new StringBuilder();
result.append(testName.getMethodName());
result.append(",");
for (String qcTestId : qcTestCases.covered())
{
result.append(qcTestId);
result.append(",");
}
System.out.println("qc tests covered: " + result);
}
}
The trick is using the “Rule” to get the testName which is available in Junit 4.5+ to get the currently executing test method.
It’s probably a bit unnecessarily complex of an example, since “related” is something extra I added for my own analysis.
This code is available on the QCIntegration repository on github.

[...] http://fijiaaron.wordpress.com/2011/06/01/integrating-junit-tests-with-hpmercury-quality-center/ http://fijiaaron.wordpress.com/2011/06/01/integrating-junit-with-hp-quality-center-part-2/ http://fijiaaron.wordpress.com/2011/11/16/upload-seleniumjunit-test-results-to-quality-center/ http://fijiaaron.wordpress.com/2011/11/17/updating-test-results-in-qc-using-the-qc-ota-api-explained/ http://fijiaaron.wordpress.com/2011/11/28/getting-a-qc-test-coverage-report-from-junit/ [...]
By: Selenium QC integration summary « Fiji Ecuador Seattle Greece Montana on February 2, 2012
at 8:51 am
[...] Integrating JUnit tests with HP/Mercury Quality Center Integrating JUnit with HP Quality Center part 2 Upload Selenium/JUnit test results to Quality Center Updating test results in QC using the QC OTA APK explained Getting a QC test coverage report from JUnit [...]
By: Selenium QC integration update « Fiji Ecuador Seattle Greece Montana on February 2, 2012
at 8:58 am