Spring @IfProfileValue Annotation Example

0

Using the spring @IfProfileValue annotation we specify the profile or environment for which the test is to be enabled. If the specified profile name/value doesn’t find a match in the configured ProfileValueSource then the test will be disabled.

@IfProfileValue can be applied at the class level or method level or both. Note that the class level annotation overrides the method level usage of the annotation.

By default the ProfileValueSource is based on the system properties, called SystemProfileValueSource. You can however implement your own custom profile source by implementing ProfileValueSource. You can also configure @IfProfileValue for multiple values.

In this article we will see examples of @IfProfileValue annotation.

@IfProfileValue Example

In the below test class, we have tests based on the database used. We have implemented a custom profile value source TestProfileValueSource which implements ProfileValueSource. It returns “oracle” for key “database”.

SpringIfProfileValueExample:

package com.javarticles.spring;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.annotation.IfProfileValue;
import org.springframework.test.annotation.ProfileValueSource;
import org.springframework.test.annotation.ProfileValueSourceConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.javarticles.spring.SpringIfProfileValueExample.TestProfileValueSource;

@RunWith(SpringJUnit4ClassRunner.class)
@ProfileValueSourceConfiguration(TestProfileValueSource.class)
@TestExecutionListeners( {})
public class SpringIfProfileValueExample {
    
    @Test
    @IfProfileValue(name="database", value="oracle")
    public void runOnOracle() {
        System.out.println("Run on oracle");
    }
    
    @Test
    @IfProfileValue(name="database", value="mysql")
    public void runOnMysql() {
        System.out.println("Run on mysql");
    }
    
    @Test
    @IfProfileValue(name="database", values={"mysql", "oracle"})
    public void runOnMysqlOrOracle() {
        System.out.println("Run on mysql or oracle");
    }
    
    @Test
    @IfProfileValue(name="database", values={"mysql", "sqlserver"})
    public void runOnMysqlOrSqlserver() {
        System.out.println("Run on mysql or sqlserver");
    }
    
    public static class TestProfileValueSource implements ProfileValueSource {

        public String get(String key) {
            return key.equals("database") ? "oracle" : "unknown";
        }
        
    }
}

Since the database value is ‘oracle’, only those tests are which are has ‘oracle’ in the profile values.

Output:

Feb 05, 2016 9:49:53 PM org.springframework.test.context.support.DefaultTestContextBootstrapper buildMergedContextConfiguration
INFO: Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.javarticles.spring.SpringIfProfileValueExample]
Feb 05, 2016 9:49:53 PM org.springframework.test.context.support.DefaultTestContextBootstrapper getTestExecutionListeners
INFO: Using TestExecutionListeners: []
Run on mysql or oracle
Run on oracle

Class level @IfProfileValue overrides method level

Class level @IfProfileValue overrides the one configured at method level.

SpringIfProfileValueClassOverrideExample:

package com.javarticles.spring;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.annotation.IfProfileValue;
import org.springframework.test.annotation.ProfileValueSource;
import org.springframework.test.annotation.ProfileValueSourceConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.javarticles.spring.SpringIfProfileValueClassOverrideExample.TestProfileValueSource;

@RunWith(SpringJUnit4ClassRunner.class)
@ProfileValueSourceConfiguration(TestProfileValueSource.class)
@TestExecutionListeners( {})
@IfProfileValue(name="database", values={"sqlserver", "oracle"})
public class SpringIfProfileValueClassOverrideExample {
    
    @Test
    @IfProfileValue(name="database", value="oracle")
    public void runOnOracle() {
        System.out.println("Run on oracle");
    }
    
    @Test
    @IfProfileValue(name="database", value="mysql")
    public void runOnMysql() {
        System.out.println("Run on mysql");
    }
    
    @Test
    @IfProfileValue(name="database", values={"mysql", "oracle"})
    public void runOnMysqlOrOracle() {
        System.out.println("Run on mysql or oracle");
    }
    
    @Test
    @IfProfileValue(name="database", values={"mysql", "sqlserver"})
    public void runOnMysqlOrSqlserver() {
        System.out.println("Run on mysql or sqlserver");
    }
    
    public static class TestProfileValueSource implements ProfileValueSource {

        public String get(String key) {
            return key.equals("database") ? "oracle" : "unknown";
        }
        
    }
}

Output:

Feb 05, 2016 9:52:56 PM org.springframework.test.context.support.DefaultTestContextBootstrapper buildMergedContextConfiguration
INFO: Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.javarticles.spring.SpringIfProfileValueClassOverrideExample]
Feb 05, 2016 9:52:56 PM org.springframework.test.context.support.DefaultTestContextBootstrapper getTestExecutionListeners
INFO: Using TestExecutionListeners: []
Run on mysql or oracle
Run on oracle

Download the source code

This was an example about spring IfProfileValue annotation.

You can download the source code here: springIfProfileValueAnnotationExample.zip
Share.

Comments are closed.