Santhosh Kumar
Santhosh Kumar

Reputation: 313

Gradle run test classes in parallel with methods of same class running in same thread

I'm having a few Junit test classes. I want to run them in 2 threads, so included maxParallelForks = 2 . I want to make sure that tests of same class run in same thread sequentially. How to achieve this? (I use SpringRunner.)

Upvotes: 0

Views: 758

Answers (2)

Santhosh Kumar
Santhosh Kumar

Reputation: 313

I was using @RunWith(Suite.class) to run multiple test classes. So I created a new Runner class and this solved my problem.

public class ParallelExecutor extends Suite {

    public ParallelExecutor(Class<?> klass, RunnerBuilder builder) throws InitializationError, IOException, InterruptedException {
        super(klass, builder);

        setScheduler(new RunnerScheduler() {

            private final ExecutorService service = Executors.newFixedThreadPool(10);

            public void schedule(Runnable childStatement) {
                service.submit(childStatement);
            }

            public void finished() {
                try {
                    service.shutdown();
                    service.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
                } catch (InterruptedException e) {
                    e.printStackTrace(System.err);
                }
            }
        });
    }
}

Upvotes: 1

Vampire
Vampire

Reputation: 38639

As far as I can see from a quick look at the Gradle sources, this should exactly be the option you want. maxParallelForks make test classes be executed in parallel, not single test methods.

Upvotes: 0

Related Questions