you can write a class that implements TestRules, and define test rules in it, what's being written before and after every test (you can also add measuring test time and other stuff), like this class:
package Test;
import java.io.IOException;
import java.io.OutputStream;
import java.text.DecimalFormat;
import org.junit.rules.ExternalResource;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
public class TestRulesSetter implements TestRule {
private OutputStream out = null;
private final TestCasePrinter printer = new TestCasePrinter();
private String beforeContent = null;
private String afterContent = null;
private long timeStart;
private long timeEnd;
public TestRulesSetter(OutputStream os) {
out = os;
}
private class TestCasePrinter extends ExternalResource {
@Override
protected void before() throws Throwable {
timeStart = System.currentTimeMillis();
out.write(beforeContent.getBytes());
};
@Override
protected void after() {
try {
timeEnd = System.currentTimeMillis();
double seconds = (timeEnd-timeStart)/1000.0;
out.write((afterContent+"Time elapsed: "+new DecimalFormat("0.000").format(seconds)+" sec\n").getBytes());
} catch (IOException ioe) { /* ignore */
}
};
}
public final Statement apply(Statement statement, Description description) {
beforeContent = "\n[TEST START] "+description.getMethodName()+"\n"; // description.getClassName() to get class name
afterContent = "[TEST ENDED] ";
return printer.apply(statement, description);
}
}
and then in your test case, you can add an instance of that test rule setter and pass System.out to it with the annotation @Rule like the following:
package Test;
import static org.junit.Assert.*;
import org.junit.Rule;
import org.junit.Test;
public class rsdg {
@Rule
public TestRulesSetter pr = new TestRulesSetter(System.out);
@Test
public void test1() {
}
@Test
public void test2() {
}
@Test
public void test3() {
}
}
this way gives you a lot of control on formatting the way your test look like
check out this article:
http://technicaltesting.wordpress.com/2012/10/23/junit-rule-for-printing-test-case-start-and-end-information/