私は、引数を提供し、クラスのmainメソッドを実行するテストケースを持っています。 Junitを使用して、複数のスレッドを同時に実行して、クラスのmainメソッドを実行する最良の方法は何でしょうか。複数のスレッドを分割する
4
A
答えて
1
なぜあなたはそれをしますか? は本当にが複数のスレッドで実行されていますか?奇妙なデザインだと私は確信しています。
一方、プログラムの並列実行をテストする場合(別々のJVM内で)は、マルチスレッドと同じではなく、JUnitは同じプログラム内で実行するので、実行しませんJVM。それでも問題はありませんが、違いを知っていることを確認してください。
SO上のいくつかの例:
- Concurrent JUnit testing
- How do I test a concurrent Java program which expects cmd line arguments?(別のJVMで並列テスト実行のためのいくつかの他のツール)
7
ないTestNGはあなたのためのオプションですが、それはだかどうかわからそれとかなり簡単です:
@Test(invocationCount = 100, threadPoolSize = 10)
public void myTest() { ... }
これにより、テストメソッドが10種類の異なるスレッドから100回呼び出されます。このテストが合格し、それをたくさん実行すると、テスト対象のコードがマルチスレッドセーフであることがかなり確信できます。ここで
0
は軽量なソリューションです:ここでは
は、あなたがテストするクラスである:ここでは
package mTTest;
/**
* UUT class is the Unit Under Test. This will be tested.
* It has two simple method:
* push(): sets the message string if it's null, and waits otherwise.
* pop(): if there is any message sets it null and returns it.
*
*/
public class UUT {
String message = null;
synchronized void push(String msg){
while (null != message) {
try {
wait();
} catch (InterruptedException e) {
}
}
message = msg;
notifyAll();
}
synchronized String pop(){
while (null == message) {
try {
wait();
} catch (InterruptedException e) {
}
}
String ret = message;
message = null;
notifyAll();
return ret;
}
}
は、Testクラスです。これはJUnitフレームワークによって呼び出されます。 multiTest()メソッドを書き直してください。 パッケージmTTest;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
import org.junit.Test;
/**
* This is the JUnit test class. Method in this class will invoked by the JUnit
* framework.
*/
public class DUTTest {
/**
* Stores sub test threads errors.
*/
private static List<AssertionError> errors;
/**
* sub test threads call this function with they errors.
* @param err
*/
static void handle(AssertionError err){
errors.add(err);
}
/**
* Simpler single thread test
* @throws InterruptedException
*/
@Test
public void testSingle() {
UUT dut = new UUT();
dut.push("hello");
assertEquals("set-get", "hello", dut.message);
}
/**
* Complex multi-thread test
* @throws InterruptedException
*/
@Test
public void testMulti() throws Exception {
/*
* Initialization
*/
errors = Collections.synchronizedList(new ArrayList<AssertionError>());
UUT dut = new UUT();
MyTestThread th = new MyTestThread(dut);
/*
* Tests
*/
dut.push("hello");
assertEquals("set-get", "hello", dut.message);
th.start();
dut.push("hello");
th.join();
/*
* Error handling
*/
ListIterator<AssertionError> iter = errors.listIterator(errors.size());
while (iter.hasPrevious()) {
AssertionError err = iter.previous();
err.printStackTrace();
if(iter.previousIndex() == -1){
throw err;
}
}
}
}
ここにはスレッドがありますが、これは数回呼び出すことができます。オーバーライドtest()メソッド。
package mTTest;
import static org.junit.Assert.assertEquals;
/**
* This is the custom test thread class. The main test thread (which is started
* by JUnit) starts this thread.
*
*/
public class MyTestThread extends Thread {
UUT dut;
/**
* Constructor
* @param dut : should be overwritten to your custom DUT-class
*/
public MyTestThread(UUT dut) {
this.dut =dut;
}
/**
* run() method is final to prevent overriding. Override test instead.
* It just calls the test method and handle the assertion errors.
*/
@Override
public final void run() {
try{
test();
} catch (AssertionError ex){
DUTTest.handle(ex);
}
}
/**
* Write your tests here. run calls this function.
*/
void test(){
assertEquals("set-get", "This will cause an ERROR", dut.pop());
assertEquals("set-get", "hello", dut.pop());
}
}
関連する問題
- 1. 複数のスレッドに作業を分割する
- 2. 分割dmpファイルを複数のdmpファイルに分割する
- 3. SQL複数の列を複数の行に分割する
- 4. md5を分割して複数のコア/スレッドを実行できますか?
- 5. pygtkアプリケーション内のスレッドを分割する
- 6. marklogic mlcpカスタム変換分割ファイルを複数のファイルに分割
- 7. 文字列を複数の部分に分割する
- 8. webappを複数の部分に分割する(tomcat)
- 9. RDDを複数のRDDSに分割
- 10. スレッド間で不均等数を分割する
- 11. バイナリの構文:複数パターンの分割
- 12. jQueryを使用して複数のリストを複数のリストに分割する
- 13. Excel VBA:複数のシートに分割
- 14. pdftk分割pdf複数のページで
- 15. ManyToManyFieldをModelFormの複数のフォームフィールドに分割する
- 16. 単一のデータベースフィールドを複数のフィールドに分割する
- 17. Polymerのページを複数のファイルに分割する
- 18. タグ上の文字列を複数のテキストボックスに分割する
- 19. 複数のテーブル間でNHibernateエンティティの行ストレージを分割する
- 20. 1つのページを複数のファイルに分割する
- 21. 複数ページのTIFFを別々のページに分割する
- 22. 1つの列を複数の列に分割する
- 23. 複数のカンマ区切りの列を行に分割する
- 24. 複数行の行を別の行に分割する
- 25. バッシュ:複数行に分割ライン
- 26. 複数のファイルにjQueryプラグインを分割する方法
- 27. 複数ページのASCIIファイルを分割する
- 28. データを複数のテーブルに分割する
- 29. BizTalkソリューションを複数のプロジェクトに分割する理由
- 30. Scalaスクリプトを複数のファイルに分割する方法
私の問題を解決しました。ありがとう.. –