Skip to content

Instantly share code, notes, and snippets.

@beargiles
Created August 3, 2015 00:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beargiles/a3418e4b496983c14cc5 to your computer and use it in GitHub Desktop.
Save beargiles/a3418e4b496983c14cc5 to your computer and use it in GitHub Desktop.
How to add database logging to JUnit3 TestCase.
public class AdvancedTestCase extends TestCase {
public void run(TestResult result) {
AdvancedTestListener l = AdvancedTestListenerFactory.INSTANCE.newInstance();
result.addListener(l);
l.setName(getName());
// normally do this with reflection
MyTestedClass orig = ((MyAdvancedTest) this).obj;
MyFacadeClass facade = new MyFacadeClass(orig);
((MyAdvancedTest) this).obj = facade;
super.run(result);
((MyAdvancedTest) this).obj = orig;
result.removeListener(l);
}
}
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import junit.framework.AssertionFailedError;
import junit.framework.Test;
import junit.framework.TestListener;
/**
* Simple test listener that logs junit tests to database (or JMS topic, etc.).
*/
public class AdvancedTestListener implements TestListener {
private static final TimeZone UTC = TimeZone.getTimeZone("UTC");
private long start;
private boolean successful = true;
private String name;
AdvancedTestListener() {
}
public void setName(String name) {
this.name = name;
}
public void startTest(Test test) {
start = System.currentTimeMillis();
}
public void addError(Test test, Throwable t) {
// cache information about error.
successful = false;
}
public void addFailure(Test test, AssertionFailedError e) {
// cache information about failure.
successful = false;
}
/**
* After the test finishes we can update the database with statistics about
* the test - name, elapsed time, whether it was successful, etc.
*
* This method writes the results to stdout since we should all know how to
* write the info to the database via JDBC or framework.
*/
public void endTest(Test test) {
long elapsed = System.currentTimeMillis() - start;
SimpleDateFormat fmt = new SimpleDateFormat();
fmt.setTimeZone(UTC);
// write information as before.
System.out.printf("[<id>, %s, %s, %s, %d, %s]\n", test.getClass().getName(), name, fmt.format(new Date(start)),
elapsed, Boolean.toString(successful));
// add captured inputs and outputs
if (test instanceof MyAdvancedTest) {
MyTestedClass obj = ((MyAdvancedTest) test).obj;
if (obj instanceof MyFacadeClass) {
MyFacadeClass facade = (MyFacadeClass) obj;
System.out.printf("[<id>, <test_id>, %s, %s, %s]\n", facade.getA(), facade.getB(), facade.getResult());
}
}
// write any information about errors or failures to database.
}
}
/**
* TestListener factory that pre-initializes an expensive resource such as a
* database or JMS connection. Instances should get a threadsafe resource.
*/
public class AdvancedTestListenerFactory {
public static final AdvancedTestListenerFactory INSTANCE = new AdvancedTestListenerFactory();
public AdvancedTestListenerFactory() {
// establish connection here?
}
public AdvancedTestListener newInstance() {
AdvancedTestListener listener = new AdvancedTestListener();
return listener;
}
}
public class MyFacadeClass extends MyTestedClass {
private MyTestedClass parent;
private String a;
private String b;
private String result;
public MyFacadeClass(MyTestedClass parent) {
this.parent = parent;
}
public String getA() {
return a;
}
public String getB() {
return b;
}
public String getResult() {
return result;
}
/**
* Wrap tested method so we can capture input and output.
*/
public String op(String a, String b) {
this.a = a;
this.b = b;
String result = parent.op(a, b);
this.result = result;
return result;
}
}
/**
* Run tests using the 'simple' test listener.
*/
public class MySimpleTest extends SimpleTestCase {
private MyTestedClass obj = new MyTestedClass();
public void test1() {
assertEquals("a:b", obj.op("a", "b"));
}
public void test2() {
assertEquals(":b", obj.op(null, "b"));
}
public void test3() {
assertEquals("a:", obj.op("a", null));
}
public void test4() {
assertEquals(":", obj.op(null, null));
}
}
/**
* Simple class to be tested
*/
public class MyTestedClass {
public String op(String a, String b) {
return ((a == null) ? "" : a) + ":" + ((b == null) ? "" : b);
}
}
public class SimpleTestCase extends TestCase {
public void run(TestResult result) {
SimpleTestListener l = SimpleTestListenerFactory.INSTANCE.newInstance();
result.addListener(l);
l.setName(getName());
super.run(result);
result.removeListener(l);
}
}
public class SimpleTestListener implements TestListener {
private static final TimeZone UTC = TimeZone.getTimeZone("UTC");
private long start;
private boolean successful = true;
private String name;
private String failure = null;
SimpleTestListener() {
}
public void setName(String name) {
this.name = name;
}
public void startTest(Test test) {
start = System.currentTimeMillis();
}
public void addError(Test test, Throwable t) {
// cache information about error.
successful = false;
}
public void addFailure(Test test, AssertionFailedError e) {
// cache information about failure.
failure = e.getMessage();
successful = false;
}
/**
* After the test finishes we can update the database with statistics about
* the test - name, elapsed time, whether it was successful, etc.
*
* This method writes the results to stdout since we should all know how to
* write the info to the database via JDBC or framework.
*/
public void endTest(Test test) {
long elapsed = System.currentTimeMillis() - start;
SimpleDateFormat fmt = new SimpleDateFormat();
fmt.setTimeZone(UTC);
System.out.printf("[%s, %s, %s, %d, %s, %s]\n", test.getClass().getName(), name, fmt.format(new Date(start)),
elapsed, failure, Boolean.toString(successful));
// write any information about errors or failures to database.
}
}
/**
* TestListener factory that pre-initializes an expensive resource such as a
* database or JMS connection. Instances should get a threadsafe resource.
*/
public class SimpleTestListenerFactory {
public static final SimpleTestListenerFactory INSTANCE = new SimpleTestListenerFactory();
private DataSource ds;
public SimpleTestListenerFactory() {
// establish connection here?
}
public SimpleTestListener newInstance() {
SimpleTestListener listener = new SimpleTestListener();
return listener;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment