Integration test exception in Eclipse IDE using JUnit 4

I have two types of tests in my Java (Maven) web project: β€œregular” unit tests and integration tests using the built-in Tomcat 7 server and Selenium to automatically test the GUI on Jenkins. All tests are annotated using JUnit @Test , normal tests end with "Test.java", and integration tests end with "IntegrationTest.java". All test classes are located in src / test / java

I usually create my project using mvn clean verify , while the corresponding part of my pom.xml , which starts the tomcat server and breaks up the test categories, looks like this:

  <!-- For front-end testing --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <uriEncoding>UTF-8</uriEncoding> <additionalConfigFilesDir>${basedir}/conf</additionalConfigFilesDir> <contextFile>${basedir}/src/test/resources/context.xml</contextFile> </configuration> <executions> <execution> <id>start-tomcat</id> <phase>pre-integration-test</phase> <goals> <goal>run-war-only</goal> </goals> <configuration> <fork>true</fork> <port>9090</port> </configuration> </execution> <execution> <id>stop-tomcat</id> <phase>post-integration-test</phase> <goals> <goal>shutdown</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.16</version> <configuration> <excludes> <exclude>**/*IntegrationTest*</exclude> </excludes> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>2.16</version> <configuration> <includes> <include>**/*IntegrationTest*</include> </includes> </configuration> <executions> <execution> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> </execution> </executions> </plugin> 

This procedure works fine except when I want to run my tests in eclipse, where I usually right-click my project -> run as -> JUnit Tests. Selecting this option, all my tests (including integration tests) are run. In this case, the integration tests fail because Tomcat does not work (it runs only in the Maven pre-integration-test phase).

How can I exclude these tests in Eclipse using the JUnit plugin?

+5
source share
1 answer

I am using junit-toolbox . It provides annotations for unit testing and integration using lookup patterns.

 <dependency> <groupId>com.googlecode.junit-toolbox</groupId> <artifactId>junit-toolbox</artifactId> <version>2.2</version> <scope>test</scope> </dependency> 

The base package under, for example, /src/test/java/base contains two classes -

AllUnitTests.java

 package base; import org.junit.runner.RunWith; import com.googlecode.junittoolbox.ParallelSuite; import com.googlecode.junittoolbox.SuiteClasses; /** * This detects all (fast running) unit test classes by the given naming pattern. * */ @RunWith(ParallelSuite.class) @SuiteClasses({ "**/*Test.class", "!**/*IntegrationTest.class", "!**/*LearningTest.class" }) public class AllUnitTests { } 

and AllIntegrationTests.java

 package base; import org.junit.runner.RunWith; import com.googlecode.junittoolbox.SuiteClasses; import com.googlecode.junittoolbox.WildcardPatternSuite; /** * This detects all integration test classes by the given naming pattern. * */ @RunWith(WildcardPatternSuite.class) @SuiteClasses({ "**/*IntegrationTest.class", "**/*IT.class" }) public class AllIntegrationTests { } 

You can run both common test suites through Eclipse.

To be able to run tests through Maven, I use an approach similar to the one you showed.

+2
source

Source: https://habr.com/ru/post/1239067/


All Articles