title: why this is not working - Unit testing a synchronous method using parameterized data of Junit?
body: <p>I am trying to learn the JUnit and wanted to extend it to test in a multi-threaded way.<br></p>
  
  <p>The class I want to test is PrimeNumberValidator. This just tests if a passed in number is prime or not. <br></p>
  
  <pre><code>package com;
  
  public class PrimeNumberValidator {
      public Boolean validate(final Integer primeNumber) {
          System.out.println("Validating .............:" + primeNumber);
          for (int i = 2; i &lt; (primeNumber / 2); i++) {
              if (primeNumber % i == 0) {
                  return false;
              }
          }
  
          return true;
      }
  }
  </code></pre>
  
  <p><br>The PrimeNumberValidatorTest is the test class. The 2 of the test data are wrong and I have done this purposely to test for failure. <br>
  The test method testPrimeNumberValidator runs perfectly fine. But, the multi-threaded version <br>testMultiThreadedPrimeNumberValidator always says 'pass' for even wrong data. 
  <br>Why is this happening like this? <br>How do solve this ?<br></p>
  
  <pre><code>package com;
  
  import static org.junit.Assert.assertEquals;
  
  import java.util.Arrays;
  import java.util.Collection;
  import java.util.concurrent.ExecutorService;
  import java.util.concurrent.Executors;
  
  import org.junit.Before;
  import org.junit.Ignore;
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.junit.runners.Parameterized;
  
  @RunWith(Parameterized.class)
  public class PrimeNumberValidatorTest {
      private Integer primeNumber;
      private Boolean expectedValidation;
      private PrimeNumberValidator primeNumberValidator;
  
      @Before
      public void initialize() {
      primeNumberValidator = new PrimeNumberValidator();
      }
  
      // Each parameter should be placed as an argument here
      // Every time runner triggers, it will pass the arguments from parameters we defined
      public PrimeNumberValidatorTest(Integer primeNumber, Boolean expectedValidation) {
      this.primeNumber = primeNumber;
      this.expectedValidation = expectedValidation;
      }
  
      @Parameterized.Parameters
      public static Collection primeNumbers() {
      return Arrays.asList(new Object[][] {
          { 2, Boolean.FALSE},// 2 is prime so Test should fail
          { 6, Boolean.FALSE}, //is NOT prime so test should pass
          { 19, Boolean.TRUE},//is prime so test should pass
          { 22, Boolean.TRUE} //is NOT prime so test should fail
      });
      }
  
      // This test will run 4 times since we have 4 parameters defined
      @Test
      public void testPrimeNumberValidator() {
      assertEquals(expectedValidation, primeNumberValidator.validate(primeNumber));
      }
  
      @Test
      public void testMultiThreadedPrimeNumberValidator() {
  
      ExecutorService executor = Executors.newFixedThreadPool(100);
      executor.submit(new Runnable() {
  
          public void run() {
          for (int i = 0; i &lt; 100; i++) {
              assertEquals(expectedValidation, primeNumberValidator.validate(primeNumber));
          }   
          }
      }); 
      }
  }
  </code></pre>
  
  <p>Referring to <br> <a href="http://www.youtube.com/watch?v=wDN_EYUvUq0" rel="nofollow noreferrer">http://www.youtube.com/watch?v=wDN_EYUvUq0</a> as mentioned in one of the posts <br> <a href="https://stackoverflow.com/questions/4039873/weird-problem-using-junit-in-multi-thread-environment">Weird problem using JUnit in multi-thread environment</a>, tried as below. <BR>The exceptions are thrown, but the JUnit does not report failure :(  <br></p>
  
  <pre><code>package com;
  
  import static org.junit.Assert.assertEquals;
  
  import java.util.Arrays;
  import java.util.Collection;
  import java.util.concurrent.ExecutorService;
  import java.util.concurrent.Executors;
  
  import org.junit.After;
  import org.junit.Before;
  import org.junit.Ignore;
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.junit.runners.Parameterized;
  
  @RunWith(Parameterized.class)
  public class PrimeNumberValidatorTest {
      volatile Exception exception;
      volatile Error error;
  
      private Integer primeNumber;
      private Boolean expectedValidation;
      private PrimeNumberValidator primeNumberValidator;
  
      @Before
      public void initialize() {
      primeNumberValidator = new PrimeNumberValidator();
      }
  
      // Each parameter should be placed as an argument here
      // Every time runner triggers, it will pass the arguments from parameters we defined
      public PrimeNumberValidatorTest(Integer primeNumber, Boolean expectedValidation) {
      this.primeNumber = primeNumber;
      this.expectedValidation = expectedValidation;
      }
  
      @Parameterized.Parameters
      public static Collection primeNumbers() {
      return Arrays.asList(new Object[][] {
          { 2, Boolean.FALSE},// 2 is prime so Test should fail
          { 6, Boolean.FALSE}, //is NOT prime so test should pass
          { 19, Boolean.TRUE},//is prime so test should pass
          { 22, Boolean.TRUE} //is NOT prime so test should fail
      });
      }
  
      // This test will run 4 times since we have 4 parameters defined
      @Test
      @Ignore
      public void testPrimeNumberValidator() {
      assertEquals(expectedValidation, primeNumberValidator.validate(primeNumber));
      }
  
      @Test
      public void testMultiThreadedPrimeNumberValidator() {
  
      ExecutorService executor = Executors.newFixedThreadPool(100);
      executor.submit(new Runnable() {
  
          public void run() {
          for (int i = 0; i &lt; 1; i++) {
              try{
                  assertEquals(expectedValidation, primeNumberValidator.validate(primeNumber));
              }catch(Error e){
                  System.out.println("error thrown :" + e);
                  error =e;
              }catch(Exception e){
                  exception=e;
                  System.out.println("exception thrown :" + e);
              }
          }   
          }
      }); 
      }
  
      @After
      public void runAfterEveryTest() throws Exception{
      if(null != error){
          System.out.println("runAfterEveryTest throwing error...............");
          throw error;
      }
      if(null != exception){
          System.out.println("runAfterEveryTest throwing exception...............");
          throw exception;
      }
      }
  }
  </code></pre>