1. 程式人生 > >多執行緒單元測試總結

多執行緒單元測試總結

多執行緒單元測試

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()); } } }