White Box Testing
Test your code
   Home      JUnit4-Multithreading-And-Concurrency-Testing
 

Concurrency testing with mycila-junit

To run a test cases on test class concurrently run your class with ConcurrentJunitRunner.classrunner class. And use annotation with @RunWithvalue as number of threads. ConcurrentJunitRunner implements a custom RunnerScheduler which delegates to a thread pool and Java Concurrent API each test method. Thus all test are executed concurrently and the RunnerScheduler waits for all tests to finish

 
package ConcurrencyTest;

 

import static org.junit.Assert.*;

import org.junit.After;

import org.junit.AfterClass;

import org.junit.Before;

import org.junit.BeforeClass;

import org.junit.Test;

import org.junit.runner.RunWith;

 

import com.mycila.junit.concurrent.Concurrency;

import com.mycila.junit.concurrent.ConcurrentJunitRunner;

 

@RunWith(ConcurrentJunitRunner.class)

@Concurrency(value = 3)

public class CalculatorTest {

 

      @BeforeClass

      public static void setUpBeforeClass() throws Exception {

      }

 

      @AfterClass

      public static void tearDownAfterClass() throws Exception {

      }

 

      @Before

      public void setUp() throws Exception {

      }

 

      @After

      public void tearDown() throws Exception {

      }

 

      @Test

      public final void testAdd() {

            Calculator cal = new Calculator();

            long result = cal.add(3, 4);

            assertEquals("actual not matching with expected", result, 7);

      }

 

      @Test

      public final void testSubtract() {

            Calculator cal = new Calculator();

            long result = cal.subtract(10, 5);

            assertEquals("actual not matching with expected", result, 5);

      }

 

      @Test

      public final void testMultiply() {

            Calculator cal = new Calculator();

            long result = cal.multiply(10, 5);

            assertEquals("actual not matching with expected", result, 50);

      }

 

      @Test

      public final void testDivide() {

            Calculator cal = new Calculator();

            long result = cal.divide(10, 5);

            assertEquals("actual not matching with expected", result, 2);

      }

 

      @Test(expected = ArithmeticException.class)

      public void exceptionTest() {

            Calculator cal = new Calculator();

            cal.divide(1, 0);

      }

}
 
 

If you have test suite consisting of many class, you would have to run your test suite with ConcurrentSuiteRunner. This runner will run all the tests in your suite test class concurrently If a test class uses the ConcurrentJunitRunner and or is annotated by @Concurrency , Otherwise it will be run test class sequentially.

 
package ConcurrencyTest;

 

import org.junit.runner.RunWith;

import org.junit.runners.Suite;

import com.mycila.junit.concurrent.ConcurrentSuiteRunner;

 

@RunWith(ConcurrentSuiteRunner.class)

@Suite.SuiteClasses({

  CalculatorTest.class

})

 

public class CalcSuite {

    // the class remains empty,

}
 
 

External References