Reputation: 438
I want to test multiple files using JUnit test in Spring Framework. So far my progress is I can read files from directory using Spring Framework global.properties. My test class reads files from the directory and in @Before I read those files and send one by one to my @Test function to generate test results.
@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);
}
}
So in the @Test I can test each file one by one in the test. What I want to do is test all the files concurrently without waiting to finish each file. I know there is TestSuite which can execute all the test inside TestSuite. In my case this is not useful. Is there any way to create @Test programmatically without using any annotation @Test ??Thanks
Upvotes: 2
Views: 2558
Reputation: 11267
I have used Parameterized tests in the past, with good results, and there is this which allows you to use @RunWith(Parallelized.class)
to make it work.
Here is the example:
@RunWith(Parallelized.class)
public class FeatureTest
{
// same class body as above
}
All you need is a simple extension of the Parameterized runner:
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());
}
}
More info here http://hwellmann.blogspot.com/2009/12/running-parameterized-junit-tests-in.html
Upvotes: 1