您的位置:軟件測(cè)試 > 開(kāi)源軟件測(cè)試 > 開(kāi)源單元測(cè)試工具 > junit
使用反射+注解實(shí)現(xiàn)類似JUnit的效果
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2016/5/3 11:32:51 ] 推薦標(biāo)簽:單元測(cè)試 軟件測(cè)試工具

  比較特殊的是rate,用于提供一種概率性的運(yùn)行方式。
  3、框架核心代碼
package com.sigh.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Created by sigh on 2015/6/10.
*/
public class SimulationFacade {
interface RunMethod {
void run();
double getRate();
}
interface ReportMethod {
void report();
}
private static List<Object> classes = null;
private static List<RunMethod> runMethods = null;
private static List<ReportMethod> reportMethods = null;
private final static int MAX_OPERATION_TIMES = 100;
static {
classes = new ArrayList<>();
ApplicationContext applicationContext = new FileSystemXmlApplicationContext("src/spring-config.xml");
Map<String, Object> beanNames = applicationContext.getBeansWithAnnotation(Simulation.class);
for (Object o : beanNames.values()) {
classes.add(o);
}
System.out.println(beanNames);
runMethods = new ArrayList<RunMethod>();
reportMethods = new ArrayList<ReportMethod>();
for (final Object o : classes) {
Method[] methods = o.getClass().getDeclaredMethods();
for (final Method method : methods) {
if (method.isAnnotationPresent(Run.class)) {
runMethods.add(new RunMethod() {
@Override
public void run() {
try {
method.invoke(o);
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
@Override
public double getRate() {
return method.getAnnotation(Run.class).rate();
}
});
} else if (method.isAnnotationPresent(Report.class)) {
reportMethods.add(new ReportMethod() {
@Override
public void report() {
try {
method.invoke(o);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
});
}
}
}
}
public void run() {
double rate = Math.random();
for (RunMethod method : runMethods) {
if (rate <= method.getRate()) {
method.run();
break;
} else {
rate -= method.getRate();
}
}
}
public void report() {
for (ReportMethod method : reportMethods) {
method.report();
}
}
public static class MulTiThreadSimulation {
private final static int THREAD_NUM = 10;
SimulationFacade simulationFacade = new SimulationFacade();
static AtomicInteger operationTimes = new AtomicInteger(0);
public void run() {
List<Thread> threadList = new ArrayList<Thread>();
for (int i = 0; i < THREAD_NUM; i++) {
Thread thread = new Thread(new Runnable() {
@Override public void run() {
while (operationTimes.getAndIncrement() < SimulationFacade.MAX_OPERATION_TIMES) {
try {
//仿真測(cè)試
simulationFacade.run();
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
thread.start();
threadList.add(thread);
}
for (Thread thread : threadList) {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void report() {
simulationFacade.report();
}
}
public static void main(String[] args) throws InvocationTargetException, IllegalAccessException, InstantiationException {
MulTiThreadSimulation mulTiThreadSimulation = new MulTiThreadSimulation();
mulTiThreadSimulation.run();
mulTiThreadSimulation.report();
}
}
  基本的思路也相對(duì)比較清晰,所以也沒(méi)有太多需要解釋的地方。
  java的內(nèi)部類確實(shí)有很多很有意思的地方,許多地方現(xiàn)在想來(lái)還是有些復(fù)雜。估計(jì)還需要一段時(shí)間來(lái)慢慢理解java的內(nèi)存模型了。

上一頁(yè)12下一頁(yè)
軟件測(cè)試工具 | 聯(lián)系我們 | 投訴建議 | 誠(chéng)聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd