【问题标题】:How to create a TestSuite with parameterized spring Junit tests如何使用参数化的 Spring Junit 测试创建 TestSuite
【发布时间】:2011-09-08 22:29:10
【问题描述】:

我正在尝试将我的测试捆绑在一个 TestSuite 中,它将从一个目录中提取文件并在加载 spring 上下文后运行每个文件。

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

非常感谢您的回复。

谢谢

【问题讨论】:

    标签: spring testing junit


    【解决方案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;
        }
    }
    

    它不是最好的解决方案,但它确实有效。如果您有更好的想法,请发布。

    【讨论】:

      【解决方案2】:

      最近找到this解决方案。在我看来稍微好一点,因为它不依赖已弃用的代码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-11
        • 1970-01-01
        • 1970-01-01
        • 2015-05-10
        • 2019-01-04
        • 1970-01-01
        • 2023-03-13
        • 1970-01-01
        相关资源
        最近更新 更多