Piotr Pradzynski
Piotr Pradzynski

Reputation: 4535

How to prevent running tests when specific spring profile is active?

I have a Spring Boot application build on Maven. I'm using Spring profiles to distinguish environment-specific configuration. I would like to prevent running tests when a specific Spring profile is active. Reason: I would like to prevent running tests with production properties (spring.profiles.active=prod). I would like to do this globally (maybe with some Maven plugin) instead of on each test separately.

Do you have any checked solutions for this?

Upvotes: 0

Views: 1665

Answers (2)

Planck Constant
Planck Constant

Reputation: 1524

You can use @IfProfileValue annotation. But you have to add some values to your active profile and read it with mentioned annotation. You can read more here (section 3.4.3): https://docs.spring.io/spring/docs/current/spring-framework-reference/testing.html#integration-testing

EDIT: Another solution is to exclude all (or selected tests) tests in the Surefire plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <excludes>
         <exclude>${exclude.tests}</exclude>
        </excludes>
      </configuration>
</plugin>
...
<profile>
  <id>prod</id>
  <properties>
    <exclude.tests>**/*.*</exclude.tests>
  </properties>
</profile>

And then when you run mvn clean test -Pprod all tests will be skipped

Upvotes: 0

Shahzeb Khan
Shahzeb Khan

Reputation: 3642

<profiles>
    <profile>
        <id>prod</id>
        <properties>
            <maven.test.skip>true</maven.test.skip>
        </properties>
    </profile>
</profiles>

Upvotes: 1

Related Questions