savvato-software / tribe-app-backend

3 stars 20 forks source link

Research and Configure separating Unit and Integration tests #272

Closed mrsbluerose closed 3 months ago

mrsbluerose commented 3 months ago

Working through these recommendations:

Incorporating both unit and integration tests is a great approach to ensure the robustness of your application. Here's how you can organize your tests and set up your project to distinguish between unit and integration tests.

  1. Directory Structure It's a good practice to separate unit tests and integration tests into different directories. Here's a suggested structure:
src/
└── main/
    └── java/
        └── com/
            └── savvato/
                └── tribeapp/
                    └── (Your main code here)
└── test/
    └── java/
        └── com/
            └── savvato/
                └── tribeapp/
                    └── unit/
                    │   └── (Unit tests here)
                    └── integration/
                        └── (Integration tests here)
  1. Naming Conventions You can use naming conventions to help distinguish between unit and integration tests:

    • Unit Tests: Use the suffix *Test.java (e.g., UserRoleServiceTest.java).
    • Integration Tests: Use the suffix *IT.java (e.g., UserRoleServiceIT.java).
  2. Maven Configuration To run unit tests and integration tests separately, you can configure Maven to recognize and execute them based on naming conventions or directory structure.

Here’s how you can configure Maven:

Unit Tests Configuration By default, Maven runs all tests in the src/test/java directory that match the pattern */Test.java.

Integration Tests Configuration You can use the maven-failsafe-plugin to handle integration tests. Here’s an example configuration in your pom.xml:

<build>
    <plugins>
        <!-- Plugin for running unit tests -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M5</version>
            <configuration>
                <includes>
                    <include>**/unit/**/*Test.java</include>
                </includes>
            </configuration>
        </plugin>

        <!-- Plugin for running integration tests -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>3.0.0-M5</version>
            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <includes>
                    <include>**/integration/**/*IT.java</include>
                </includes>
            </configuration>
        </plugin>
    </plugins>
</build>
  1. Running Tests You can run unit and integration tests separately:
  1. Best Practices

CI Integration: Integrate both unit and integration tests into your Continuous Integration (CI) pipeline. Run unit tests on every commit and integration tests less frequently (e.g., on a schedule or before merging).