SSH框架学习(十、Junit+GroboUtils进行多线程测试)
2012-12-09 16:10
260 查看
Junit4不能模拟多线程的情况,需要其他支持,我用的是GroboUtils,最新版本5,下载地址:http://groboutils.sourceforge.net/downloads.html
GroboUtils测试的代码是用网上抄来的,来源:http://www.coderli.com/multi-thread-junit-grobountils
UserDAOImplTest的代码
GroboUtils测试的代码是用网上抄来的,来源:http://www.coderli.com/multi-thread-junit-grobountils
UserDAOImplTest的代码
package demo.myssh.dao.impl; import net.sourceforge.groboutils.junit.v1.MultiThreadedTestRunner; import net.sourceforge.groboutils.junit.v1.TestRunnable; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.test.annotation.Repeat; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.*; import demo.myssh.dao.IUserDAO; import demo.myssh.model.User; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({ "file:WebRoot/WEB-INF/applicationContext.xml" }) public class UserDAOImplTest { @Autowired @Qualifier("iUserDAO") private IUserDAO userDao; @Test @Repeat(2) public void MultiRequestsTest() { // 构造一个Runner TestRunnable runner = new TestRunnable() { @Override public void runTest() throws Throwable { // 测试内容 // System.out.println("a"); userDao.save(new User("aa", "bb", "cc")); } }; int runnerCount = 2; // Rnner数组,相当于并发多少个。 TestRunnable[] trs = new TestRunnable[runnerCount]; for (int i = 0; i < runnerCount; i++) { trs[i] = runner; } // 用于执行多线程测试用例的Runner,将前面定义的单个Runner组成的数组传入 MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(trs); try { // 开发并发执行数组里定义的内容 mttr.runTestRunnables(); } catch (Throwable e) { e.printStackTrace(); } } }
相关文章推荐
- SSH框架学习(八、Junit+GroboUtils进行多线程测试)
- SSH框架学习(十、Junit+GroboUtils进行多线程测试)
- JUnit学习笔记7---mock objects进行孤立测试1
- JUnit学习笔记9---mock object进行孤立测试3
- Junit如何进行多线程测试
- (软件项目管理)在eclipse里使用junit进行单元测试以及Maven的学习
- 学习使用junit进行单元测试,
- ssh框架学习-struts2 使用自定义拦截器进行文字过滤拦截
- JUnit学习笔记8---mock object进行独立测试2
- 使用Junit进行耗时多线程测试
- JUnit学习笔记14---对JSP和taglib进行单元测试1
- JUnit学习笔记16---用JSP和taglib进行单元测试3
- JUnit学习笔记19---对数据库应用程序进行单元测试3
- JUnit学习笔记20---对数据库应用程序进行单元测试4
- JUnit学习笔记17---对数据库应用程序进行单元测试1
- JUnit学习笔记20---对数据库应用程序进行单元测试4
- JUnit学习笔记18---对数据库应用程序进行单元测试2
- JUnit学习笔记10---mock object进行孤立测试4
- JUnit4学习笔记2-Eclipse中使用JUint4进行单元测试
- Junit如何进行多线程测试