Skip to content

JUnit5 callback reporting example

Ivan edited this page Nov 27, 2019 · 3 revisions

JUnit5 callback reporting example

Usage of callback reporting is represented in this test class.

Basic steps to use callback reporting feature in your test classes:

You can either provide storage for retrieved TestItemLeaf like in JUnit4 example or send requests right after each test method execution using the JUnit5 @AfterEach method passing TestInfo as argument.

@AfterEach
void afterMethod(TestInfo testInfo) {
	TestItemTree.TestItemLeaf testItemLeaf = ItemTreeUtils.retrieveLeaf(testInfo, TEST_ITEM_TREE);
	if (testItemLeaf != null) {
               attachLog(testItemLeaf);
               finishWithStatus(testInfo.getDisplayName().contains("someTest") ? "FAILED" : "PASSED", testItemLeaf);
	}
}

TestItemLeaf can be retrieved using ItemTreeUtils util.

Send Log using ItemTreeReporter sendLog method:

private static void attachLog(TestItemTree.TestItemLeaf testItemLeaf) {
	ItemTreeReporter.sendLog(REPORT_PORTAL.getClient(),
			"ERROR",
			"Error message",
			Calendar.getInstance().getTime(),
			testItemLeaf
	);
}

Send finish request for TestItem using ItemTreeReporter finishItem method:

private void finishWithStatus(String status, TestItemTree.TestItemLeaf testItemLeaf) {
	FinishTestItemRQ finishTestItemRQ = new FinishTestItemRQ();
	finishTestItemRQ.setStatus(status);
	finishTestItemRQ.setEndTime(Calendar.getInstance().getTime());
	ItemTreeReporter.finishItem(REPORT_PORTAL.getClient(), finishTestItemRQ, TEST_ITEM_TREE.getLaunchId(), testItemLeaf)
			.cache()
			.subscribe();
}

TestItem finish method returns Maybe<String> so you should either provide consumer for result or use blockingGet to provide request sending before test run finish (application termination).

Clone this wiki locally