junit执行之后会有一个结果展示,下面就来看一下怎么获取这些结果并将其存储为一个对象
junit代码如下:
package test;import org.junit.After;import org.junit.Assert;import org.junit.Before;import org.junit.Test;public class JunitDemo { @Before public void bofore() { System.out.println("bofore"); } @After public void after() { System.out.println("after"); } @Test public void test1() { System.out.println("test1"); Assert.assertEquals(1, 2); } @Test public void test2() { System.out.println("test2"); Assert.assertEquals(1, 1); } @Test public void test3() { System.out.println("test3"); Integer.valueOf("aede21"); }}
首先先写两个类,分别是结果对象和结果中的方法执行结果对象
结果记录类:
package test;import java.util.List;public class MyResultRecorder { String script_name; Listlist; Boolean result; public String getScript_name() { return script_name; } public void setScript_name(String script_name) { this.script_name = script_name; } public List getList() { return list; } public void setList(List list) { this.list = list; } public Boolean getResult() { return result; } public void setResult(Boolean result) { this.result = result; } @Override public String toString() { return "MyResultRecorder [script_name=" + script_name + ", list=" + list + ", result=" + result + "]"; }}
记录中的方法:
package test;public class MethodInfo { String method_name; Boolean result; String error_msg; public String getMethod_name() { return method_name; } public void setMethod_name(String method_name) { this.method_name = method_name; } public Boolean getResult() { return result; } public void setResult(Boolean result) { this.result = result; } public String getError_msg() { return error_msg; } public void setError_msg(String error_msg) { this.error_msg = error_msg; } @Override public String toString() { return "MethodInfo [method_name=" + method_name + ", result=" + result + ", error_msg=" + error_msg + "]"; }}
然后,需要写一个监听器ExecutionListener,继承junit的RunListener,并在监听时给对象赋值
package test;import java.util.ArrayList;import java.util.List;import org.junit.runner.Description;import org.junit.runner.Result;import org.junit.runner.notification.Failure;import org.junit.runner.notification.RunListener;public class ExecutionListener extends RunListener { MyResultRecorder recorder; MethodInfo methodInfo; Listlist; public ExecutionListener() { this.list = new ArrayList<>(); } public void testRunStarted(Description description) throws Exception { System.out.println("--------- START ----------"); recorder = new MyResultRecorder(); } public void testRunFinished(Result result) throws Exception { recorder.setResult(result.wasSuccessful()); recorder.setList(list); System.out.println("--------- END ----------"); System.out.println("执行结果 : " + result.wasSuccessful()); System.out.println("执行时间 : " + result.getRunTime()); System.out.println("执行数量 : " + result.getRunCount()); System.out.println("失败数量 : " + result.getFailureCount()); System.out.println("忽略数量 : " + result.getIgnoreCount()); } public void testStarted(Description description) throws Exception { recorder.setScript_name(description.getClassName()); System.out.println(description.getMethodName() + " begin"); methodInfo = new MethodInfo(); methodInfo.setMethod_name(description.getMethodName()); } public void testFinished(Description description) throws Exception { System.out.println(description.getMethodName() + " end"); if (methodInfo.getError_msg() == null) methodInfo.setResult(true); list.add(methodInfo); } public void testFailure(Failure failure) throws Exception { System.out.println("Execution of test case failed : " + failure.getMessage()); methodInfo.setResult(false); methodInfo.setError_msg(failure.getMessage()); } public void testIgnored(Description description) throws Exception { }}
写一个junit的类,做多个执行的处理
package test;import org.junit.After;import org.junit.Assert;import org.junit.Before;import org.junit.Test;public class JunitDemo2 { @Before public void bofore() { System.out.println("bofore"); } @After public void after() { System.out.println("after"); } @Test public void test2() { System.out.println("test2"); Assert.assertEquals(1, 1); }}
最后,写一个执行类,看一下执行的效果
package test;import org.junit.runner.JUnitCore;public class Execute { public static void main(String[] args) { run(JunitDemo.class, JunitDemo2.class); } private static void run(Class ... classes) { for (Class clazz : classes) { JUnitCore runner = new JUnitCore(); ExecutionListener listener = new ExecutionListener(); runner.addListener(listener); runner.run(clazz); MyResultRecorder recorder = listener.recorder; System.out.println(recorder); } }}
执行的结果如下:
--------- START ----------
test1 beginboforetest1afterExecution of test case failed : expected:<1> but was:<2>test1 endtest2 beginboforetest2aftertest2 endtest3 beginboforetest3afterExecution of test case failed : For input string: "aede21"test3 end--------- END ----------执行结果 : false执行时间 : 11执行数量 : 3失败数量 : 2忽略数量 : 0MyResultRecorder [script_name=test.JunitDemo, list=[MethodInfo [method_name=test1, result=false, error_msg=expected:<1> but was:<2>], MethodInfo [method_name=test2, result=true, error_msg=null], MethodInfo [method_name=test3, result=false, error_msg=For input string: "aede21"]], result=false]--------- START ----------test2 beginboforetest2aftertest2 end--------- END ----------执行结果 : true执行时间 : 1执行数量 : 1失败数量 : 0忽略数量 : 0MyResultRecorder [script_name=test.JunitDemo2, list=[MethodInfo [method_name=test2, result=true, error_msg=null]], result=true]
这样就通过重写junit的监听,将junit的执行结果,存储到一个对象当中