title: Spring Task Executor created extra threads in JUNIT
body: <p>I have simple task executor in my spring-config.xml and couple of chains attached </p>
  
  <pre><code>      &lt;channel id="inputChannel" /&gt;
      &lt;task:executor id="threadPoolExecutor" pool-size="2" /&gt;
      &lt;publish-subscribe-channel id="multiCastChannel"
          task-executor="threadPoolExecutor" /&gt;
  
      &lt;chain input-channel="inputChannel"
          output-channel="multiCastChannel"&gt;
          &lt;json-to-object-transformer
              type="com.company.integration.domain.DomainObject" /&gt;
          &lt;service-activator ref="validator"
              method="validate" /&gt;
  &lt;/chain&gt;
  
  &lt;chain input-channel="multiCastChannel"
          output-channel="inventoryAdjustmentOutputChannelOne"&gt;
          &lt;service-activator ref="adapterOne"
              method="buildOutputMessageOne" /&gt;
  &lt;/chain&gt;
  &lt;chain input-channel="multiCastChannel"
          output-channel="inventoryAdjustmentOutputChannelTwo"&gt;
          &lt;service-activator ref="adapterTwo"
              method="buildOutputMessageTwo" /&gt;
  &lt;/chain&gt;
  </code></pre>
  
  <p>When a message posted to "inputChannel" and after processing and sent to "multuCastChannel" there are two threads created without issues like</p>
  
  <p><strong>threadPoolExecutor-1</strong>
  <strong>threadPoolExecutor-2</strong></p>
  
  <p>And these two are created only once per input message which is fine.
  But when I am trying to test the same with JUnit... each "multiCastChannel" chains are executing twice...<strong><em>means my service activators (adapterOne,adapterTwo) in the chains are calling twice</em></strong> per chain ...which is wired..</p>
  
  <p>Any idea why JUnit has this behavior ?</p>
  
  <p>Below is the Junit piece of code</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = { "classpath:configuration/spring-config.xml"})
  @FixMethodOrder(MethodSorters.NAME_ASCENDING)
  @PropertySource("classpath:application.properties")
  @SuppressWarnings("unchecked")
  @WebAppConfiguration
  @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
  public class InventoryAdjustmentMessageTest {
  
  
      @Autowired
      private DirectChannel inputChannel;
  
  
      @Test
      public void testTaskShed()
              throws IOException, InterruptedException, JMSException {
          String validInput = setup("valid-message.txt");
          Message&lt;String&gt; inputMessage = TextMessageUtil.createNewGenericMessage(validInput);
          inputChannel.send(inputMessage);
          Thread.sleep(5000);
  </code></pre>
  
  <h2>        }</h2>
  
  <p><strong><em>Spring integration version : 4.1.6</em></strong></p>
  
  <p>Adding application config information: </p>
  
  <pre><code>@Import({ HarnessConfiguration.class, LocalConfiguration.class, MongoDbConfiguration.class, WebConfiguration.class,
          WebsphereMQJMSConfiguration.class })
  @Configuration
  @EnableWebMvc
  @ComponentScan(basePackages = { "com.abc.inventory.adjustment.integration.service",
          "com.abc.inventory.adjustment.integration.domain" }, useDefaultFilters = false, excludeFilters = {
                  @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = ApplicationConfiguration.class) }, includeFilters = {
                          @ComponentScan.Filter(type = FilterType.ANNOTATION, value = { Controller.class,
                                  Component.class }) })
  @ImportResource({ "classpath:configuration/spring-config.xml", "classpath:configuration/spring-adapters.xml" })
  @EnableMongoRepositories("com.abc.inventory.adjustment.integration.service.audit.repository")
  @EnableAutoConfiguration
  @EnableMongoAuditing
  public class ApplicationConfiguration extends WebMvcConfigurerAdapter {
  //
  </code></pre>
  
  <h2>    }</h2>
  
  <p>Adding debug snapshot
  <a href="https://i.stack.imgur.com/JLP6C.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JLP6C.png" alt="Extra threads for publish-subscribe channel in JUNIT"></a></p>
  
  <p>-Tej</p>