2016년 12월 1일 목요일

Junit MultiThread Test code


import org.junit.Test;

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

public class TestThread extends TestCase{
 
 private static int THREAD_SIZE = 20;
 
 /*
  * Test class should have exactly one public zero-argument constructor이므로
  * TestCase 클래스를 extends 하고 GroboUtils의 클래스인 TestRunnable을 상속받은 클래스를 이너클래스로 사용하였다.
  */
 
 public class MultiThreadTest extends TestRunnable {
  
  private int i = 0;
  
  
  public MultiThreadTest(int i){
   this.i = i;
  }
  
  @Override
  public void runTest() throws Throwable {
   Thread.sleep(1000);
   
   System.out.println(Thread.currentThread().getName() + " : [" + i + "]");
  }
 }
 
 @Test
 public void test() throws Throwable{
  TestRunnable[] t = new TestRunnable[THREAD_SIZE];
  
  for(int index=0; index < THREAD_SIZE; index++){
   t[index]=new MultiThreadTest(index);
  }
  
  MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(t);
  mttr.runTestRunnables();
  
  System.out.println("main end.........................");
 }
}