2011-09-08 15 views
1

TestSuiteでテストをバンドルしようとしています。これは、ディレクトリからファイルを取得し、スプリングコンテキストを読み込んだ後にそれぞれを実行します。私はこれを行う場合はパラメータ化されたスプリングを使用してTestSuiteを作成する方法Junitテスト

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = {"/META-INF/spring/context-test.xml"}) 
public class MyTestCase extends TestCase{ 

    private String fileName; 

    public MyTestCase(String fileName){ 
     this.fileName = fileName; 
    } 

    @Resource private Processor processor; 

    @Before 
public void setup(){ 
    ... 
    } 

    @Test 
    public void test(){ 
    Read file and run test.. 
    ... 
    } 

} 

、それが認識しない春の注釈

public class MyTestSuite extends TestCase{ 

    public static Test suite(){ 
     TestSuite suite = new TestSuite(); 
     suite.addTest(new MyTestCase("file1")); 
     suite.addTest(new MyTestCase("file2")); 
     return suite; 
    } 
} 

は、私はそれを見上げるとが見つかりました:Spring 3+ How to create a TestSuite when JUnit is not recognizing itを、私はJUnit4TestAdapterを使用する必要があることを示唆しています。 JUnitTestAdapterの問題は、パラメータを渡すことができず、MyTestSuite.suite()も使用しないことです。私は次のようなことしかできません。

public class MyTestSuite{ 

    public static Test suite(){ 

     return new JUnit4TestAdapter(MyTestCase.class); 
    } 
} 

あなたの回答は高く評価されています。私はこれを達成するために非推奨AbstractSingleSpringContextTestsを使用していた

おかげ

答えて

1

。 AbstractSingleSpringContextTestsはTestContextフレームワークが利用できなかった時からのものです。

public class MyTestCase extends AbstractSingleSpringContextTests { 

    private String fileName; 

    public MyTestCase(String fileName){ 
     this.fileName = fileName; 
    } 

    @Resource private Processor processor; 

    @Override 
    protected void onSetUp(){ 

     initialization code... 

    } 

    @Override 
    protected String getConfigPath(){ 
     return "config/File/Path"; 
    } 

    @Test 
    public void test(){ 
    Read file and run test.. 
    ... 
    } 

} 


public class MyTestSuite extends TestCase{ 

    public static Test suite(){ 
     TestSuite suite = new TestSuite(); 
     suite.addTest(new MyTestCase("file1")); 
     suite.addTest(new MyTestCase("file2")); 
     return suite; 
    } 
} 

これは最適な解決策ではありませんが、機能します。より良いアイデアがあれば投稿してください。

0

最近発見されたthis溶液。私の意見では、廃止予定のコードに依存していないので、やや優れています。