0

I have two separate Junit test cases. I have designed one JSP page to get 6 values at a time from users.In servlet doPOST method, I'm designed to get these 6 values. After that I have written below code in servlet to execute Junit test cases,

Junit test cass -1 and 2:

Result result = JUnitCore.runClasses(Junit1.class);
for (Failure failure : result.getFailures())
{
    System.out.println(failure.toString());
} 

Result result = JUnitCore.runClasses(Junit2.class);
for (Failure failure : result.getFailures())
{
    System.out.println(failure.toString());
}            

With the above method, It's running one by one, not concurrently. Could anyone help me to execute both Junit test cases running at same time.

WeSt
  • 2,628
  • 5
  • 22
  • 37
Siva
  • 17
  • 1
  • 6
  • Yes Joe. Thanks for your help. I got it. with the below code base, I got the solution for running the Junit test cases concurrently. Thread t1 = new Thread(new Runnable() { Override public void run() { executeSomeCodeInP1(); } }); Thread t2 = new Thread(new Runnable() { @Override public void run() { executeSomeCodeInP2(); } }); t1.start(); t2.start(); // if you want to wait for both threads to finish before moving on, // "join" the current thread t1.join(); t2.join(); – Siva Feb 08 '15 at 11:56

1 Answers1

0

If you go with TestNG, it provides good multi thread testing out of the box and it's compatible with JUnit tests (you need to make a few changes). For example you could run a test like this:

@Test(threadPoolSize = 5, invocationCount = 10,  timeOut = 5000)
public void runConcurrently() {
  ...
}

This would mean that the doSomething() method will be invoked 10 times by 5 different threads.

Having said that, if you are talking about running multiple tests concurrently, i would suggest you take a look at ant parallel task where in within parallel tag you specify multiple tasks to run like below in parallel where each task runs in its own thread:

<target name="output-1-2-3-kill-notepad-output-4">
    <echo message="1" />
    <parallel>
        <echo message="2" />
        <exec executable="notepad.exe" timeout="3000" />
        <echo message="3" />
    </parallel>
    <echo message="4" />
</target>

So here it will run echo in two different threads and execute tag in another thread. You could do the same by specifying multiple unit tests.

SMA
  • 36,381
  • 8
  • 49
  • 73
  • Thanks Almas. But I'm confused. I'm calling those two test cases in Servlet. so that I can send the output of those cases to jsp. I do not want to do any code change in the Junit class. I want to do some code change in servlet. I was searched in internet but I got some inputs like Asynchronous processing in servlet and multithreading..but i could not get any thing.from that. – Siva Feb 08 '15 at 11:21