前言
在通常的开发中,测试永远是必不可少的。
那么在spring中怎么测试好?因为里面有ioc,也有bean托管,下面提供一个通用的测试方案,顺便连带高并发的测试方案。
测试方案
首先,新建一个测试基础类,以后的测试都要继承自这个基础类进行测试,譬如,就叫BaseTest
内容如下:
package main;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
//@ContextConfiguration({"classpath:spring-mvc.xml","classpath:spring-mybatis.xml"})
@ContextConfiguration(locations={
"classpath*:spring-mvc.xml",
"classpath*:applicationContext.xml"
}
)
public class BaseTest {
Date beginDate;
Long begin=null;
Long end=null;
Date endDate=null;
@Before
public void init() {
//--初始化spring上下文
System.out.println("初始化spring上下文中......");
//在运行测试之前的业务代码
beginDate = new Date();
begin = beginDate.getTime();
System.out.println("任务开始时间:" + beginDate);
}
@After
public void after() {
//在测试完成之后的业务代码
endDate = new Date();
end = endDate.getTime();
System.out.println("任务结束时间:" + endDate + "");
System.out.println("任务话费时间:" + (end - begin) + "毫秒");
}
/***用于测试最大并发***/
public void runMultiThreadTest(int totalThreadCount, int threadPoolSize, Runnable runnable){
{
final int threadSize= totalThreadCount;
ExecutorService executor= Executors.newFixedThreadPool(threadPoolSize);
final AtomicInteger lockCount=new AtomicInteger(threadSize);
final AtomicInteger successCount=new AtomicInteger(0);
try {
for (int i = 0; i < threadSize; i++) {
final int theThreadNumber = i;
executor.submit(new Runnable() {
@Override
public void run() {
try {
runnable.run();
successCount.incrementAndGet();
}
catch (Exception ed){
ed.printStackTrace();
}
finally {
lockCount.decrementAndGet();
}
}
});
}
while(true){
synchronized (this){
if(lockCount.intValue()>0){
;
}
else{
break;
}
}
}
System.out.println("注意当前线程池最大线程数量为"+threadPoolSize+"个");
System.out.println("共运行线程"+threadSize+"个,成功运行线程:"+successCount.get()+"个");
}
catch (Exception ed){
ed.printStackTrace();
}
}
}
/***用于测试任务阻塞的任务队列执行下的性能***/
public void runMultiThreadByBlockQueue(int threadCount,TaskProducer producer,TaskConsumer consumer){
final LinkedBlockingQueue<TaskOutLine> queue=new LinkedBlockingQueue<>(threadCount);
final int threadSize=threadCount;
ExecutorService executor= Executors.newFixedThreadPool(threadSize);
final AtomicInteger lockCount=new AtomicInteger(threadSize);
final AtomicInteger successCount=new AtomicInteger(0);
try {
/***线程池同时产生任务队列***/
for (int i = 0; i < threadSize; i++) {
final int theThreadNumber = i;
TaskOutLine tmpOutLine=new TaskOutLine();
tmpOutLine.taskIndex=theThreadNumber;
executor.submit(new Runnable() {
@Override
public void run() {
try {
tmpOutLine.taskData=producer.produce();
queue.put(tmpOutLine);
}
catch (Exception ed){
ed.printStackTrace();
}
finally {
}
}
});
}
/***另起一个线程用于消费队列**/
new Thread(new Runnable() {
@Override
public void run() {
while (lockCount.get()>0){
try{
TaskOutLine currentObj=queue.take();
consumer.consume(currentObj);
successCount.incrementAndGet();
}
catch (Exception ed){
}
finally {
lockCount.decrementAndGet();
}
}
}
}).start();
while(true){
synchronized (this){
if(lockCount.intValue()>0){
;
}
else{
break;
}
}
}
System.out.println("共运行线程"+threadSize+"个,成功运行线程:"+successCount.get()+"个");
}
catch (Exception ed){
ed.printStackTrace();
}
}
public interface TaskProducer{
public Object produce();
}
public interface TaskConsumer{
public void consume(TaskOutLine taskOutLine);
}
public class TaskOutLine{
public int taskIndex=0;
public Object taskData=new Object();
public int getTaskIndex() {
return taskIndex;
}
public void setTaskIndex(int taskIndex) {
this.taskIndex = taskIndex;
}
public Object getTaskData() {
return taskData;
}
public void setTaskData(Object taskData) {
this.taskData = taskData;
}
}
}
好了,如何测试呢?
看例子:
下面根据这些来做一个入门测试:
文件结构如下:
测试代码如下:
package other;
import main.BaseTest;
import org.junit.Test;
public class TestCase1 extends BaseTest {
@Test
public void t1(){
System.out.println("你好这是测试");
}
}
测试结果:
测试可用。