Java 如何在JUnit测试中对多个文件执行测试

Java 如何在JUnit测试中对多个文件执行测试,java,junit,junit4,Java,Junit,Junit4,我想在Spring框架中使用JUnit测试来测试多个文件。到目前为止,我的进展是我可以使用springframeworkglobal.properties从目录中读取文件。我的测试类从目录和@中读取文件,然后我读取这些文件并逐个发送到@test函数以生成测试结果 @RunWith(MultiThreadedRunner.class) @ContextConfiguration(locations = {"/applicationContext.xml"}) public class FullCy

我想在Spring框架中使用JUnit测试来测试多个文件。到目前为止,我的进展是我可以使用springframeworkglobal.properties从目录中读取文件。我的测试类从目录和@中读取文件,然后我读取这些文件并逐个发送到@test函数以生成测试结果

@RunWith(MultiThreadedRunner.class)
@ContextConfiguration(locations = {"/applicationContext.xml"})
public class FullCycleTest {


@Before  
public void beforeUploadTest() throws Exception {


    new TestContextManager(getClass()).prepareTestInstance(this); // TestContextManager is responsible for managing Spring TestContext Framework
    this.resourcesTest = new ResourcesTest();   // get instance of ResourceTest class
    this.uploadTest = new UploadandJobTest();
    this.folder = new File(resourcesTest.basePath());
    this.file = new ArrayList<>();
    File[] fileEntry = folder.listFiles();
    this.uploadControllerTest = new UploadControllerTest();
    this.uploadResult = new UploadJSON();

    /******File upload read*****/

    for (int i = 0; i < fileEntry.length; i++) {
        if (fileEntry[i].exists()) { 
            file.add(String.valueOf(fileEntry[i]));
            System.out.println("Testing for file" + file);

        }
    }
}

@Test
public void UploadandJobTest() {

    for (String s : file) {

        uploadTest.uploadAndJobTest(s);
        uploadControllerTest.upload_stl_response(s);
    }
}
@RunWith(multi-threadedrunner.class)
@ContextConfiguration(位置={“/applicationContext.xml”})
公共类全周期测试{
@以前
public void beforeUploadTest()引发异常{
新建TestContextManager(getClass()).prepareTestInstance(this);//TestContextManager负责管理SpringTestContext框架
this.resourcesTest=new resourcesTest();//获取ResourceTest类的实例
this.uploadTest=新的UploadandJobTest();
this.folder=新文件(resourcesTest.basePath());
this.file=new ArrayList();
File[]fileEntry=folder.listFiles();
this.uploadControllerTest=新的uploadControllerTest();
this.uploadResult=new UploadJSON();
/******文件上传读取*****/
for(int i=0;i

所以在@Test中,我可以在测试中一个接一个地测试每个文件。我想做的是在不等待完成每个文件的情况下同时测试所有文件。我知道有TestSuite可以在TestSuite中执行所有测试。在我的情况下,这是没有用的。有没有办法在不使用任何注释的情况下以编程方式创建@Testanks

我在过去使用过参数化测试,效果很好,这允许您使用
@RunWith(Parallelized.class)
使其工作

以下是一个例子:

@RunWith(Parallelized.class)
public class FeatureTest
{
   // same class body as above
}
您所需要的只是参数化运行程序的简单扩展:

public class Parallelized extends Parameterized
{

    private static class ThreadPoolScheduler implements RunnerScheduler
    {
        private ExecutorService executor; 

        public ThreadPoolScheduler()
        {
            String threads = System.getProperty("junit.parallel.threads", "16");
            int numThreads = Integer.parseInt(threads);
            executor = Executors.newFixedThreadPool(numThreads);
        }

        @Override
        public void finished()
        {
            executor.shutdown();
            try
            {
                executor.awaitTermination(10, TimeUnit.MINUTES);
            }
            catch (InterruptedException exc)
            {
                throw new RuntimeException(exc);
            }
        }

        @Override
        public void schedule(Runnable childStatement)
        {
            executor.submit(childStatement);
        }
    }

    public Parallelized(Class klass) throws Throwable
    {
        super(klass);
        setScheduler(new ThreadPoolScheduler());
    }
}
更多信息请点击这里