編譯、運(yùn)行該測(cè)試方法,使用Run as Application,控制臺(tái)可能輸出如下結(jié)果,有2個(gè)Error:
…..EE
Time: 0.053
There were 2 errors:
1) CompareDateTime(com.loggingselenium.JUnitPerfTestDateUtil)java.lang.RuntimeException: 解析日期時(shí)間格式出錯(cuò),期望的字符串格式為[yyyyMMdd HH:mm:ss]
at com.loggingselenium.DateUtil.compareDateTime(DateUtil.java:18)
at com.loggingselenium.JUnitPerfTestDateUtil.CompareDateTime(JUnitPerfTestDateUtil.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at com.clarkware.junitperf.TestFactory.run(TestFactory.java:83)
at com.clarkware.junitperf.ThreadedTest$TestRunner.run(ThreadedTest.java:75)
at java.lang.Thread.run(Unknown Source)
2) CompareDateTime(com.loggingselenium.JUnitPerfTestDateUtil)java.lang.RuntimeException: 解析日期時(shí)間格式出錯(cuò),期望的字符串格式為[yyyyMMdd HH:mm:ss]
at com.loggingselenium.DateUtil.compareDateTime(DateUtil.java:18)
at com.loggingselenium.JUnitPerfTestDateUtil.CompareDateTime(JUnitPerfTestDateUtil.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at com.clarkware.junitperf.TestFactory.run(TestFactory.java:83)
at com.clarkware.junitperf.ThreadedTest$TestRunner.run(ThreadedTest.java:75)
at java.lang.Thread.run(Unknown Source)
FAILURES!!!
Tests run: 5, Failures: 0, Errors: 2
多線程并發(fā)測(cè)試失敗,我們的比較日期時(shí)間大小的方法存在線程不安全的問(wèn)題,我們需要在DateUtil中方法前加上線程同步關(guān)鍵字synchronized:
public synchronized static int compareDateTime(String dateTime1, String dateTime2) {......}
再次運(yùn)行單元測(cè)試方法,可以發(fā)現(xiàn)多線程并發(fā)下存在的問(wèn)題得到了解決。
本文轉(zhuǎn)自:http://www.cr173.com/html/18903_1.html