2011-09-08 28 views
1

我想將TestSuite中的測試捆綁在一個TestSuite中,它將從一個目錄中選取文件並在加載spring上下文後運行每個文件。如何使用參數化的春季Junit測試創建一個TestSuite

@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.. 
    ... 
    } 

} 

如果我這樣做,它不承認Spring註解

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); 
    } 
} 

您的反應非常感謝。

感謝

回答

1

我不得不使用過時AbstractSingleSpringContextTests實現這一目標。 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解決方案。在我看來稍好一些,因爲它不依賴於已棄用的代碼。

相關問題