멀티스레드를 Junit에서 테스트하기입니다.
회사에서 테스트케이스를 만들다가 부딫혀서 포스트를 올렸었는데
Green님께서 원인과 좋은 레퍼런스 사이트를 알려주셨습니다.
정말 감사합니다.
간단하게 레퍼런스 사이트 (http://today.java.net/pub/a/today/2003/08/06/multithreadedTests.html)를 보고
이곳에서 소개하고 있는 GroboUtils를 사용해서 적용해보았습니다.
잘 돌아가네요 ^^
역시.. 생각만하면 있을만한 것은 다 있는 것 같습니다..
저 글이 2003년도에 쓰여진 것으로 나오니까... 부끄럽습니다.. 한참 멀은 것 같기도 하고..
처음에 안되던게 이거였습니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Tester implements Runnable { | |
private Log logger = LogFactory.getLog(Tester.class); | |
int i = 0; | |
public Tester(int i ) { | |
this.i = i; | |
} | |
public void run() { | |
try { | |
Thread.sleep(1000); | |
} catch (InterruptedException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
System.out.println(Thread.currentThread().getName() + " : [" + i + "]"); | |
} | |
} | |
public class TestThread extends TestCase { | |
private Log logger = LogFactory.getLog(TestThread.class); | |
@Test | |
public void testM() { | |
for(int index = 0; index < 20; index++) { | |
new Thread(new Tester(index)).start(); | |
} | |
System.out.println("main end........................."); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Tester extends TestRunnable { //net.sourceforge.groboutils.junit.v1.TestRunnable를 상속 | |
private Log logger = LogFactory.getLog(Tester.class); | |
int i = 0; | |
public Tester(int i ) { | |
this.i = i; | |
} | |
@Override | |
public void runTest() { //net.sourceforge.groboutils.junit.v1.TestRunnable의 runTest메서드를 오버라이드 | |
//이 메서드는 Runnable의 run 메서드가 해야 할 일이 들어갑니다. | |
try { | |
Thread.sleep(1000); | |
} catch (InterruptedException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
System.out.println(Thread.currentThread().getName() + " : [" + i + "]"); | |
} | |
} | |
public class TestThread extends TestCase { | |
private Log logger = LogFactory.getLog(TestThread.class); | |
@Test | |
public void testM() throws Throwable { | |
TestRunnable[] t = new TestRunnable[20]; //TestRunnable로 배열을 만들고 | |
for(int index = 0; index < 20; index++) { | |
t[index] = new Tester(index); //TestRunnable을 상속한 Runnable의 run 메서드를 테스트 할 클래스를 넣습니다. | |
} | |
MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(t); //이렇게 생성하고 | |
mttr.runTestRunnables(); //이렇게 테스트 합니다. | |
System.out.println("main end........................."); | |
} | |
} |
아쉬운대로 join을 사용해서 결과만 보고 있었는데 groboutils를 사용하면 해결이 가능했습니다.
사이트는 : groboutils.sourceforge.net 입니다.
이 라이브러리를 사용해서 변경한 코드는 이렇습니다.
Thread-7 : [7]
Thread-5 : [5]
Thread-3 : [3]
Thread-1 : [1]
Thread-4 : [4]
Thread-8 : [8]
Thread-0 : [0]
Thread-6 : [6]
Thread-2 : [2]
Thread-10 : [10]
Thread-15 : [15]
Thread-11 : [11]
Thread-14 : [14]
Thread-18 : [18]
Thread-9 : [9]
Thread-13 : [13]
Thread-16 : [16]
Thread-12 : [12]
Thread-17 : [17]
Thread-19 : [19]
main end.........................
위 참고 사이트에 나와있는 방식은 TestRunnable을 상속한 클래스를 내부 클래스로 선언해서 사용하는 것이었습니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TestThread extends TestCase { | |
public class TesterInner extends TestRunnable { | |
int i = 0; | |
public TesterInner(int i ) { | |
this.i = i; | |
} | |
@Override | |
public void runTest() { | |
try { | |
Thread.sleep(1000); | |
} catch (InterruptedException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
System.out.println(Thread.currentThread().getName() + " : [" + i + "]"); | |
} | |
} | |
@Test | |
public void testM() throws Throwable { | |
TestRunnable[] t = new TestRunnable[20]; | |
for(int index = 0; index < 20; index++) { | |
t[index] = new TesterInner(index); | |
} | |
MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(t); | |
mttr.runTestRunnables(); | |
System.out.println("main end........................."); | |
} | |
} |