2011-09-08 27 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解决方案。在我看来稍好一些,因为它不依赖于已弃用的代码。

相关问题