Note that there are some explanatory texts on larger screens.

plurals
  1. PORunning a test with threadPoolSize > 1 and triggering a @BeforeMethod only once
    primarykey
    data
    text
    <p>I want to run a test that uses a singelton resource <code>MyResource</code> which can be created and teared down. The resource represents another running program on the same machine which is controlled via command line <code>Runtime.exec</code>. The program must be explicitly started and shut down, therefore, calling <code>MyResource.establishConnection</code> twice will fail. Failing to call <code>MyResource.shutDown</code> will result in the application being open even after the Java program finishes. Now I want to write a test case which checks if the resource can be used after it was started. (Starting and shutting down is tested elsewhere.) The test case I wrote looks like this:</p> <pre><code>private MyResource myResource; @BeforeMethod(firstTimeOnly = true) public void setUp() throws Exception { myResource = new MyResource(); myResource.establishConnection(); } @AfterMethod(lastTimeOnly = true) public void tearDown() throws Exception { myResource.shutDown(); } @Test(invocationCount = 30, threadPoolSize = 5) public void testMethod() throws Exception { myResource.use(): } </code></pre> <p>One of the purposes of this test is to check if the resource (<code>MyResource</code>) behaves nicely when it is accessed concurrently. However, I now observe that the method annotated with <code>@BeforeMethod</code> is called three times - once for every thread - instead of only once. The <code>firstTimeOnly</code> seems to not apply to positive<code>threadPoolSize</code>s.</p> <p>Can I fix this? (There are other tests in this test class which are represented by other test methods. This is why I rather do not want to use for example <code>@BeforeTest</code> such that the application is restarted in between methods.)</p> <p>Thanks for any help.</p> <p><strong>Update</strong>: More mysteries. This code snippet</p> <pre><code>@BeforeMethod(firstTimeOnly = true) public void setUp() throws Exception { LOGGER.info("Set up"); } @AfterMethod(lastTimeOnly = true) public void tearDown() throws Exception { LOGGER.info("Tear down"); } @Test(invocationCount = 10) public void testName() throws Exception { LOGGER.info("Running test"); } </code></pre> <p>will print:</p> <blockquote> <p>Set up </p> <p>Running test </p> <p>Tear down </p> <p>Running test </p> <p>Running test </p> <p>Running test</p> <p>Running test </p> <p>Running test </p> <p>Running test </p> <p>Running test </p> <p>Running test</p> <p>Running test</p> </blockquote> <p>So not only is <code>setUp</code> called three times as described above, <code>tearDown</code> is called too early, even without setting <code>threadPoolSize</code>. This is in direct contrast to the javadoc of <code>@AfterMethod</code>'s <code>lastTimeOnly</code> which says:</p> <blockquote> <p>If true and the @Test method that was just run has an invocationCount > 1, this AfterMethod will only be invoked once (after the last test invocation).</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload