1

I need to run Junit test methods in a Class parallelly to save time using Threads. I have added the following code in my pom.xml file after referring to a website. please let me know what the next step i need to do run the test methods parallelly.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.18.1</version>
    <configuration>
        <parallel>classesAndMethods</parallel>
        <perCoreThreadCount>false</perCoreThreadCount>
        <useUnlimitedThreads>true</useUnlimitedThreads>
        <threadCountMethods>5</threadCountMethods>
        <includes>
            <include>${tests}</include>
        </includes>
    </configuration>
</plugin>
1

You can use the ANT parallel task to achieve this parallelism. Following is the demo which shows how to execute testcases parallel with Selenium Grid and JUnit.

Download the demo

  1. Download the selenium grid from [here][1] and extract it into some folder
  2. Now navigate to extracted folder through command prompt and type following command

    ant launch-hub

This will launch the hub on port 4444, this is the port where your selenium tests need to connect.

  1. Now you need to launch the remote control. type the following command on command prompt

    ant launch-remote-control

This will launch the remote control on port 5555.

  1. Now we have launched one remote control, we will launch another remote control on port 5556. So one class will execute all it's test on remote control running on 5555 and another class will run it's testcases on remote control running on 5556. Type the following command to launch the remote control on 5556 port.

    ant launch-remote-control -Dport=5556

  2. Now we have two remote control running on the same machine we need to run testcase

demo1.java

import static org.junit.Assert.assertTrue;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;

public class demo1 {
 Selenium selenium;

 @Before
 public void setUp() throws Exception {
  selenium = new DefaultSelenium("localhost", 4444, "*firefox", "<URL>");
  selenium.start();
  selenium.setTimeout("6000");
 }

 @Test
 public void test_1() throws Exception {
        // write the test code here
 }
 @Test
 public void test_2() throws Exception {
        // write the test code here
 }

 @After
 public void tearDown() throws Exception {
   selenium.stop();
 }
}

demo2.java

import static org.junit.Assert.assertTrue;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;

public class demo2 {
 Selenium selenium;

 @Before
 public void setUp() throws Exception {
  selenium = new DefaultSelenium("localhost", 4444, "*firefox", "<URL>");
  selenium.start();
  selenium.setTimeout("6000");
 }

 @Test
 public void test_3() throws Exception {
      // write the test code here
 }
 @Test
 public void test_4() throws Exception {
        // write the test code here;
 }

 @After
 public void tearDown() throws Exception {
   selenium.stop();
 }
}

build.xml

<project name="demo" default="run" basedir=".">
 <path id="lib.classpath">
  <fileset dir="lib">
   <include name="*.jar" />
  </fileset>
  <pathelement location="bin" />
 </path>

 <target name="run" depends="compile">
  <parallel threadCount='4'>
   <junit printsummary="withOutAndErr" haltonfailure="no">
    <formatter type="xml" usefile="true" />
    <classpath refid="lib.classpath" />
    <batchtest fork="true" todir="results" failureproperty="seleniumTests.failed" errorproperty="seleniumTests.failed">
     <fileset dir="bin">
      <include name="demo1.class" />
     </fileset>
    </batchtest> 
   </junit>


   <junit printsummary="withOutAndErr" haltonfailure="no">
   <formatter type="xml" usefile="true" />
   <classpath refid="lib.classpath" />
   <batchtest fork="true" todir="results" failureproperty="seleniumTests.failed" errorproperty="seleniumTests.failed">
    <fileset dir="bin">
     <include name="demo2.class" />
    </fileset>
   </batchtest> 
   </junit>
  </parallel>
 </target>

 <target name="compile">
 <echo> compiling.....</echo>
  <javac srcdir="src" destdir="bin" classpathref="lib.classpath" />
 </target>
</project>
  1. Create demo1.java, demo2.java and build.xml as shown above
  2. Now you run the testcases using build.xml file. Use the following command to run the testcases.

ant run

This will run demo1.class and demo2.class in parallel.

1

There may be some misconfigurations in your specification. You declare useUnlimitedThreads=true but then threadCountMethods=5, and they don't seem to get along with each other:

The parameter useUnlimitedThreads allows for an unlimited number of threads. Unless useUnlimitedThreads is set to "true", the parameter threadCount can be used with the optional parameter perCoreThreadCount.

Also, I'm not sure about your <include>${tests}</include>.

Anyways, I would just start over from your requirement. Since it is pretty basic, I would recommend to try a simple configuration like the following:

<plugins>
[...]
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <configuration>
      <parallel>methods</parallel>
      <threadCount>5</threadCount>
    </configuration>
  </plugin>
[...]
</plugins>

http://maven.apache.org/surefire/maven-surefire-plugin/examples/junit.html

  • make sure to be using JUnit > 4.7 (check in your <dependencies>)
  • make sure to follow maven conventions about the name and the location of the test classes, i.e. they need to be placed under src/test/java, and the files be named XYXTest.java
  • that should be it

About parallel=classesAndMethods, which was specified in the question: I don't think you need it; I'd rather go with a simple parallel=methods, as above. I'm not 100% sure of the different behavior, as I can't find any explanation on maven surefire's web site, but I guess that with classesAndMethods you will have test methods from different classes running at the same time (on different threads). Whereas with parallel=methods you will have running at the same time only test methods from one class; that is, the runner will pick each test class, and run its methods, sequentially, in one thread. The problem with parallel=classesAndMethods is that the greater the level of concurrency, the bigger the headaches in case two test classes have some kind of dependencies (think of two test classes that use a production class which is static and maintains a static state).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.