6

I have a Maven Java project in which I added to the pom:

<build>
....
    <plugin>
                <!-- adding second test source directory (just for integration tests) -->
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>${plugin.build-helper-maven-plugin.version}</version>
                <executions>
                    <execution>
                        <id>add-integration-test-source</id>
                        <phase>generate-test-sources</phase>
                        <goals>
                            <goal>add-test-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>src/integration-test/java</source>
                            </sources>
                        </configuration>
                    </execution>
                    <execution>
                        <id>add-integration-test-resource</id>
                        <phase>generate-test-resources</phase>
                        <goals>
                            <goal>add-test-resource</goal>
                        </goals>
                        <configuration>
                            <resources>
                                <resource>
                                    <directory>src/integration-test/resources</directory>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
</build>

InteliJ recognized my java and resource folders under integration-test as a code folder, but Eclipse doesn't. Is there any way that eclipse adds these folders as code folders when the project is imported?

JSBach
  • 4,679
  • 8
  • 51
  • 98
  • Did you install M2E plugin? Then use Configure->Convert to Maven Project. Then you need to add source dir `src/integration-test/java` to the Maven Compiler Plugin. In the default directory layout you would put your test code into `src/test/java` and it works out of the box – Stephan Jan 14 '16 at 12:51
  • Yes, src/test works. I want to have both srC/test and src/intergation-tests – JSBach Jan 14 '16 at 14:48
  • Usually unit tests and intergration tests are in the same source folder. Your problem is caused by the separation of both. I suggest not using your own directory layout with Maven since this will cause many problems and you always have to configure around it. Just stick to the standard. – Stephan Jan 15 '16 at 11:50

2 Answers2

0

Try to right click on your folder in Project Explorer select Build Path option in context menu and later click Use as Source Folder in menu which appears after choosing Build Path.

Mateusz Sroka
  • 2,255
  • 2
  • 17
  • 19
  • Ok, but everyone that imports this project will have to do the same. I thught that Eclipse would be able to infer that from pom, just as InteliJ did. Is this possible? – JSBach Jan 14 '16 at 11:45
  • Eclipse ignores test resources. As far as I know, you can't work around it using Maven, unless you also add it to resources available during compilation and runtime. – Schaka Jan 14 '16 at 11:53
  • I am not sure but probably Eclipse cannot recognize folder named `integration-test`. If your folder will be named `test` Eclipse will automatically use it as source folder. – Mateusz Sroka Jan 14 '16 at 11:54
  • I have a test folder, they are for unit tests. My integration tests is for integration tests. So I can run unit tests on test phase and integration tests on IT phase. – JSBach Jan 14 '16 at 12:02
  • Please look at `best answer` in this [question](http://stackoverflow.com/questions/7160006/m2e-and-having-maven-generated-source-folders-as-eclipse-source-folders) – Mateusz Sroka Jan 14 '16 at 13:19
0

I suggest not using your own directory layout with Maven since this will cause many problems and you always have to configure around it. Just stick to the standard.

  • Separate integration tests and unit tests not by their source folders, but by their name.
  • Put all tests in src/test/java. You don't have to configure anything at this point, this path is taken by default.
  • Call integration tests IT*.java and unit tests UT*.java.
  • They can be run separately because maven-surefire-plugin executes unit tests and maven-failsafe-plugin executed integration tests. You can define filename patterns for identifying the test classes.
  • You could also create profiles for running only UTs or only ITs.

    <project>
    <!-- ... -->
    <build>
    <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.13</version>
      <configuration>
        <includes>
          <include>**/UT*.java</include>
        </includes>
        <excludes>
          <exclude>**/IT*.java</exclude>
        </excludes>
      </configuration>
    </plugin>
    
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-failsafe-plugin</artifactId>
      <version>2.18</version>
      <configuration>
        <includes>
          <include>**/IT*.java</include>
        </includes>
      </configuration>
    
      <executions>
        <execution>
          <id>failsafe-integration-tests</id>
          <phase>integration-test</phase>
          <goals>
            <goal>integration-test</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
    

Further reading: http://tomaszdziurko.pl/2013/01/running-unit-tests-integration-tests-separately-maven-testng/

There is also a interesting article about the correct usage of integration tests here: http://zeroturnaround.com/rebellabs/the-correct-way-to-use-integration-tests-in-your-build-process/

Stephan
  • 4,395
  • 3
  • 26
  • 49
  • Ok, that seems interesting, but would I be able to run all my unit tests easily without running the ITs? (From the IDe, while developing) – JSBach Jan 15 '16 at 13:40
  • You could run the tests with Maven, since it is your build system or create test suites: http://help.eclipse.org/kepler/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2FgettingStarted%2Fqs-junit.htm – Stephan Jan 15 '16 at 14:24