2016-01-06 129 views
2

我想测试使用JUnit测试中的Spring框架的多个文件。到目前为止,我的进步是我可以阅读使用Spring框架global.properties从目录中的文件。我的测试类从目录中读取文件,并在@Before我阅读这些文件,并通过一个送一个给我@Test函数生成测试结果。如何执行在JUnit测试测试多个文件

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

所以在@Test中我可以在测试中逐个测试每个文件。我想做的是同时测试所有的文件,而无需等待完成每个文件。我知道有TestSuite的可以执行的TestSuite里面所有的测试。在我的情况下,这是没有用的。有没有什么办法可以通过编程方式创建@Test而不用任何注释@Test ??谢谢

回答

1

我以前使用过参数化测试,结果很好,并且有这个允许你使用@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()); 
    } 
} 

更多的信息在这里http://hwellmann.blogspot.com/2009/12/running-parameterized-junit-tests-in.html

相关问题