7

I want to set Spring Profile in order to test different sets of JUnit tests. I tried this:

application.properties:

spring.profiles.active=dev

test 1

@IfProfileValue(name = "spring.profiles.active", values = {"test"})
@ActiveProfiles(profiles = {"test"})
public class SecondGenericTest {

    @Test
    public void secGenericTest() {

        System.out.println("Performing test ... for profile test");
    }
}

test 2

@IfProfileValue(name = "spring.profiles.active", values = {"test"})
@ActiveProfiles(profiles = {"test"})
public class SecondGenericTest {

    @Test
    public void secGenericTest() {

        System.out.println("Performing test ... for profile test");
    }
}

Tried to set also into InteliJ Active profile:

enter image description here

But every time both tests are executed. Is there some solution to run the test with the proper active profile?

POM configuration:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-bean-validators</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>org.mariadb.jdbc</groupId>
            <artifactId>mariadb-java-client</artifactId>
            <version>2.5.4</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
        </dependency>
        <dependency>
            <groupId>plugin</groupId>
            <artifactId>org.plugin</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.10.2</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.jaxrs</groupId>
            <artifactId>jackson-jaxrs-json-provider</artifactId>
            <version>2.10.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.woodstox</groupId>
            <artifactId>woodstox-core-asl</artifactId>
            <version>4.4.1</version>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-core</artifactId>
            <version>2.3.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.3.2</version>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>8.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.10.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-core</artifactId>
        </dependency>
        <dependency>
            <groupId>io.reactivex.rxjava2</groupId>
            <artifactId>rxjava</artifactId>
        </dependency>
        <!--  Dependency for for Netty. Remove it when WebFlux is not used -->
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-transport-native-epoll</artifactId>
            <version>4.1.45.Final</version>
            <classifier>linux-x86_64</classifier>
        </dependency>
        <!-- Packages for testing -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.6.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.6.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>3.2.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>5.6.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.restdocs</groupId>
            <artifactId>spring-restdocs-mockmvc</artifactId>
            <version>2.0.4.RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-engine</artifactId>
            <version>1.6.0</version>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-commons</artifactId>
            <version>1.6.0</version>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-launcher</artifactId>
            <version>1.6.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>test</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.3</version>
                <configuration>
                    <archive>
                        <manifestEntries>
                            <Dependencies>jdk.unsupported</Dependencies>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <release>12</release>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M4</version>
            </plugin>
        </plugins>
    </build>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <repositories>
        <repository>
            <id>repository.spring.release</id>
            <name>Spring GA Repository</name>
            <url>http://repo.spring.io/release</url>
        </repository>
        <repository>
            <id>sonatype-releases</id>
            <url>https://oss.sonatype.org/content/repositories/releases/</url>
        </repository>
        <repository>
            <id>java.net</id>
            <url>https://maven.java.net/content/repositories/public/</url>
        </repository>
        <repository>
            <id>JBoss repository</id>
            <url>http://repository.jboss.org/nexus/content/groups/public/</url>
        </repository>
        <repository>
            <id>jvnet-nexus-snapshots</id>
            <name>jvnet-nexus-snapshots</name>
            <url>https://maven.java.net/content/repositories/snapshots/</url>
        </repository>
    </repositories>
Peter Penzov
  • 1,126
  • 134
  • 430
  • 808

2 Answers2

2

I believe the correct syntax should be

@IfProfileValue(name = "spring.profiles.active", value = "test")

Default implementation of class, which accompanies @IfProfileValue annotation is ProfileValueSource class, which looks in system properties - therefore you need to set profile via JVM properties, i.e. --Dspring.profiles.active="test"

Unfortunately @IfProfileValue does not account for values specified via @ActiveProfiles or by @Profile

Javadocs:

Although the @IfProfileValue and @Profile annotations both involve profiles, they are not directly related. @Profile involves bean definition profiles configured in the Environment; whereas, @IfProfileValue is used to enable or disable tests.

You can see default implementation class at Github

Which looks for keys in System properties

@Override
public String get(String key) {
    Assert.hasText(key, "'key' must not be empty");
    return System.getProperty(key);
}

So we can see that actually we do not have to set spring profile specifically, it can be any environment variable, that can be resolved by System.getProperty(key); call, i.e test.active.profile for example.

Or you can create your own implementation of ProfileValueSource:

As @IfProfile looks by default in System Properties, you need to provide implementation of ProfileValueSource to read from any property source. Note below example still looks into system properties - this just an example of custom resolver.

public class MyProfileValueSource implements ProfileValueSource {
  private final Properties testProperties;

  public MyProfileValueSource() {

      ClassPathResource resource = new ClassPathResource("test.properties");
      if (resource.exists()) {
          try {
              this.testProperties = PropertiesLoaderUtils.loadProperties(resource);
          } catch (IOException e) {
              throw new RuntimeException(e);
          }
      } else {
          testProperties = new Properties();
      }
  }

  @Override
  public String get(String key) {
      return testProperties.getProperty(key, System.getProperty(key));
  }

and then refer to it in your test class

@ExtendWith(SpringRunner.class)
@ContextConfiguration(classes = MyApplicationConfig.class)
@ProfileValueSourceConfiguration(MyProfileValueSource.class)
public class ServiceTests {

  @Test
  @IfProfileValue(name = "test.enabled", value = "true")
  public void test() {

  }
}

You can provide any implementation you of ProfileValueSource you need (i.e. read from any other property source)

fg78nc
  • 4,774
  • 3
  • 19
  • 32
1

As explained by Sam Brannen in this answer:

As stated in the Javadoc, @IfProfileValue is used to indicate that a test is enabled for a specific testing profile or environment.

Whereas, @ActiveProfiles is used to declare which active bean definition profiles should be used when loading an ApplicationContext for test classes.

In other words, you use @IfProfileValue to control whether a test class or test method will be executed or skipped, and you use @ActiveProfiles to set the active bean definition profiles that will be used to load the ApplicationContext for your test.

So in your case @ActiveProfile should do the trick.

Also JUnit 5 allows you to add tags to your tests, functionality which can help you run only certain tests.

You can do this by using the @Tag("...") annotation, and set it as a parameter for your configuration, by changing the test kind to tags as you choose

Also you can configure tags via maven for building/releasing via

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0-M3</version>
        <configuration>
            <!-- include tags -->
            <groups>integration, feature-168</groups>
            <!-- exclude tags -->
            <excludedGroups>slow</excludedGroups>
        </configuration>
    </plugin>

enter image description here

Sam Brannen
  • 29,611
  • 5
  • 104
  • 136
Alex Ciocan
  • 2,272
  • 14
  • 20
  • @ActiveProfile? I get - Cannot resolve symbol 'ActiveProfile' – Peter Penzov Feb 22 '20 at 19:28
  • @ActiveProfiles, and also you should run your tests with the spring runner, by using: @ExtendWith(SpringExtension.class) – Alex Ciocan Feb 22 '20 at 19:32
  • I tried with `@ExtendWith(SpringExtension.class) @ActiveProfiles(profiles = {"test"})` but again every JUnit is run; Any idea why might be missing? – Peter Penzov Feb 22 '20 at 19:34
  • If you want to exlude only certain tests you could use the junit5 tag feature – Alex Ciocan Feb 22 '20 at 19:46
  • Can you share more what is this? – Peter Penzov Feb 22 '20 at 19:47
  • With the @Tag, you can create customized test plans. We use this for example, in our app to mark some integration tests as unstable, and skip them in a ci/cd environment. Check this https://howtodoinjava.com/junit5/junit-5-tag-annotation-example/ for a more detailed explanation – Alex Ciocan Feb 22 '20 at 19:49
  • The best way of achieving this segregation(separation) of tests(per environment:test, acceptance,production) could be by using the `@Tag` feature and just set an jvm environment variable on each machine – Alex Ciocan Feb 22 '20 at 20:06
  • ok, so I can use tag to execute specific test. Just one thing is not clear. Into InteliJ where I can set which JUnit with tag name should be executed? – Peter Penzov Feb 22 '20 at 20:18
  • I added a explanation in the answer please check it. – Alex Ciocan Feb 22 '20 at 20:29
  • Thanks. Last question: I can use tags during development to test different Junit tests when they are needed. But if I want to make final build of the jar how I can skip some of the JUnit tests which I don't want to run? – Peter Penzov Feb 22 '20 at 20:31