Question

I have such configuration:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.13</version>

            <configuration>
                <excludedGroups>integration</excludedGroups>
                <argLine>-Xmx512m -XX:MaxPermSize=128m</argLine>
                <systemPropertyVariables>
                    <project.build.directory>${project.build.directory}</project.build.directory>
                </systemPropertyVariables>
            </configuration>
        </plugin>
    </plugins>
</build>

And a test:

@Test(groups = "integration")//testng
public class MyTest extends BaseTestForMyTest {


private final SomeClass sut = new SomeClass(getStuffFromSysPropDefinedForFailSafe());//should fail in surefire.


@Test(groups = "integration")
public void someTest() throws IOException {
    //some code
 }
}

surefire tries to instantiate a class and fails (fail is ok, this test is for failsafe!). Why, why does surefire tries to instantiate test from excluded group?

Sorry, I didn't mention. I'm using testng

Was it helpful?

Solution

Two answers in one here.

Answer to first question "why is new SomeClass(getStuffFromSysPropDefinedForFailSafe()); being called?"

TestNG instantiates all the test classes before looking for annotations. I don't know if this is intentional, or if it can be avoided. TestNG is preparing to call the @BeforeClass methods, so it wants the object to call the method on. In instantiating the object, the instance members (in this case, sut) are instantiated, too.

I can see that unconditionally creating an instance of each test class makes life easier for TestNG, and quite probably noone has ever asked for any other behaviour.


Answer to second question (not sure if you're actually asking this one) "why is the test being run?"

You're not excluding it properly. In TestNG, the groups parameter takes an array of strings, not a single string. Try

@Test(groups = { "Integration" })
public class YourTests {
  @Test(groups = { "ReallySlow" })
  public void someTest {
    // This test is a really slow integration test and is in both groups.
  }
}

Not an answer to any question you asked, but hopefully of interest to some people who've read this far (plus I wrote this before some of your question edits):

To mark a JUnit test as being in a group that the surefire plugin will exclude, you have to annotate them as being in a Category. The JUnit equivalent of the above TestNG code is:

@Category(IntegrationTest.class)
public class YourTests {
  @Category(ReallySlowTest.class)
  @Test
  public void someTest {
    // This test is a really slow integration test and is in both categories.
  }
}

You can read more in this dzone article.

Also, you should read up on the failsafe plugin since that's more suitable for integration tests.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top