Is there a class/method I could implement within JUnit that is basically a "hook" that runs after each test case finishes, with user-defined (in code) metadata available?
I'm trying to accomplish something like this:
@Team(Teams.PAYMENTS)
)For simplicity, I just need to print this info (later it will get published elsewhere), but I'm really lost in where I could obtain this test metadata information in one place, after the test runs?
I tried to override the finish method in one of our TestRunner
class:
override fun finish(resultCode: Int, results: Bundle?) {
System.out.println("===breakpoint here===")
super.finish(resultCode, results)
}
but when debugging, there was no test metadata here. Any suggestions on how I could approach this?
Thank you!
For JUnit 4, you can use a TestWatcher rule:
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
public class TestWatcherAPIUnitTest {
@Rule(order = Integer.MIN_VALUE)
public TestWatcher watchman = new TestWatcher() {
@Override
protected void failed(Throwable e, Description description) {
System.out.println(description + " Team: " + getTeam(description) + " failed!");
}
@Override
protected void succeeded(Description description) {
System.out.println(description + " Team: " + getTeam(description) + " success!");
}
String getTeam(Description description) {
Team team = description.getAnnotation(Team.class);
return (team != null) ? team.value() : "unknown_team";
}
};
@Test
@Team("team1")
public void failing_test() {
Assert.assertTrue(false);
}
@Test
@Team("team2")
public void successful_test() {
Assert.assertTrue(true);
}
}
Similarly, for JUnit5 there is a TestWatcher API