多线程单元测试总结

多线程单元测试

pom配置

   <!--springmvc中进行多线程测试-->  
    <dependency>  
        <groupId>net.sourceforge.groboutils</groupId>  
        <artifactId>groboutils-core</artifactId>  
        <version>1.2.1</version>  
        <scope>system</scope>  
        <systemPath>D:/工作/学习/多线程测试/GroboTestingJUnit-1.2.1-core.jar</systemPath>  
    </dependency>  
    <!--解决javax.servlet.sessionconfig报错问题--> 
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>

单元测试代码

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import net.sourceforge.groboutils.junit.v1.MultiThreadedTestRunner;
import net.sourceforge.groboutils.junit.v1.TestRunnable;

/**
 * 
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {"/META-INF/spring/*Context.xml",
        "file:src/main/webapp/WEB-INF/ServletConfigs/AppServlet/servlet-context.xml",
        "/META-INF/spring/*-beans.xml"})
public class MultiDataSourceTest {

    @Autowired
    private IService iService;
    /**
     * 
     * 
     */
    @Test
    public void test() {

        TestRunnable[] trs = new TestRunnable[10];  
        for (int i = 0; i < trs.length; i++) {  
            trs[i] = new MuiltThread(iService);  
        }  

        MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(trs);  

        try {  
            System.out.println(Thread.currentThread().getName());  
            mttr.runTestRunnables();  
        } catch (Throwable e) {  
            e.printStackTrace();  
        } 
    }

    class MuiltThread extends TestRunnable{  
        private IService iService;

        public MuiltThread() {  
            super();  
        }  

        public MuiltThread(IService iService) {  
            super();  
            this.iService= iService;  
        }  

        public IWindIpoService getIService() {  
            return iService;  
        }  

        public void setIService(IService iService) {  
            this.iService= iService;   
        }  

        @Override  
        public void runTest() throws Throwable {  
                System.out.println(iService.testMultiDataSource()); 
        }  

    }  

}

猜你喜欢

转载自blog.csdn.net/sinat_30075299/article/details/80914222