question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Introduce annotation to skip test based on active Spring profile [SPR-11677]

See original GitHub issue

Torsten Krah opened SPR-11677 and commented

I’ve looked at the @IfProfileValue and @ActiveProfiles annotations, but neither does what I am searching for.

We are using the "spring.profiles.active" system property to activate different profiles, even at the test level – e.g. "h2, default" or something else.

We have tests which are only for Oracle and should therefore only be run if the "oracle" profile is active, but I’ve found no way to express this on the test class itself.

A dedicated annotation would be nice to support this.


Affects: 4.0.3

Issue Links:

  • #12410 Decide what to do with @IfProfileValue
  • #9538 Introduce strategy for determining if a profile value is enabled for a particular test environment
  • #13622 Allow overriding @ActiveProfiles in test classes with system property
  • #13625 SystemProfileValueSource is not very compatible with the new 3.1 default system property profiles

8 votes, 8 watchers

Issue Analytics

  • State:open
  • Created 9 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
spring-projects-issuescommented, Jan 11, 2019

Caleb Cushing commented

Why not just allow test methods and test classes to be annotated with ```java @Profile

1reaction
spring-projects-issuescommented, Jan 11, 2019

Christoph Strobl commented

Hi, we’ve had similar requirements when testing against different versions. Basically we created a custom JUnit @Rule to disbale tests for features only available in Redis 2.8 when running against 2.6.

Let me outline what I am thinking of in terms of dealing with spring profiles.

I’ve added a sample below basically adding a new annotation IfSpringProfileActive holding the desired profile name. The SpringProfileRule checks for the presence of IfSpringProfileActive, loads the current Environment if not already set, and verifies that the desired profile is currently active.

There’s SpringRuleTest at the end of the sample to show what it does.

/**
 * Annotation holding name of desired profile used to mark methods from being potentially excluded. 
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface IfSpringProfileActive {

	String value();
}

/**
 * TestRule verifying Tests marked with IfSpringProfileActive are verified against currently active spring profile
 */
public class SpringProfileRule implements TestRule {

	private Environment env;

	private SpringProfileRule(Environment env) {
		this.env = env;
	}

	public static SpringProfileRule forSpringJunitClassRunner() {
		return new SpringProfileRule(null);
	}

	public static SpringProfileRule forEnvironment(Environment env) {
		return new SpringProfileRule(env);
	}

	@Override
	public Statement apply(final Statement base, Description description) {

		IfSpringProfileActive profileValue = description.getAnnotation(IfSpringProfileActive.class);
		final String requiredProfile = profileValue != null ? profileValue.value() : null;

		return new Statement() {

			@Override
			public void evaluate() throws Throwable {

				if (StringUtils.hasText(requiredProfile)) {
					initEnvironmentWhenNotSet(base);
					verify(requiredProfile);
				}

				base.evaluate();
			}
		};

	}

	protected void initEnvironmentWhenNotSet(Statement base) {

		if (env == null && base instanceof RunAfterTestMethodCallbacks) {

			// there should be a better way of doing this...
			TestContextManager contextManager = (TestContextManager) new DirectFieldAccessor(base)
					.getPropertyValue("testContextManager");
			TestContext testContext = (TestContext) new DirectFieldAccessor(contextManager).getPropertyValue("testContext");
			env = testContext.getApplicationContext().getEnvironment();
		}
	}

	protected void verify(String requiredProfile) throws Throwable {

		if (StringUtils.hasText(requiredProfile)) {
			if (!env.acceptsProfiles(requiredProfile)) {
				throw new AssumptionViolatedException(String.format("Profile %s is currently not active", requiredProfile));
			}
		}
	}

}

/**
 * A quick test to show that it works
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@ActiveProfiles(profiles = "oracle")
public class SpringRuleTest {

	@Autowired String foo;

	public @Rule SpringProfileRule rule = SpringProfileRule.forSpringJunitClassRunner();

	@Configuration
	static class Config {

		@Bean
		String foo() {
			return "foo";
		}
	}

	@Test
	public void shouldAlwaysRun() {
		System.out.println("Run no matter what!");
	}

	@Test
	@IfSpringProfileActive("oracle")
	public void shouldOnlyRunWhenActiveProfileSetToOracle() {
		System.out.println("Runnig on oracle.");
	}

	@Test
	@IfSpringProfileActive("not-oracle")
	public void shouldBeIgnoredIfActiveProfileSetToOracle() {
		System.out.println("Oracle should not see me.");
	}

}
Read more comments on GitHub >

github_iconTop Results From Across the Web

how to run/turn off selective tests based on profiles in spring boot
You would want to use the @IfProfileValue annotation. Unfortunately it doesn't work directly on the active profiles but it can read a property...
Read more >
Setting Default Spring Profile for Tests with Override Option
So we decided to have all Spring tests running with test profile. Setting test profile. There are few ways to setup active profiles...
Read more >
Don't Use the @Profile Annotation in a Spring Boot App!
This configuration adds a bean of type MockService to the application context if the test profile is active, and a bean of type...
Read more >
Spring Profiles - Baeldung
In this tutorial, we'll focus on introducing Profiles in Spring. ... Tests make it very easy to specify what profiles are active using...
Read more >
How to use Spring Profiles - Tutorial with Examples - amitph
Examples of Spring and Spring Boot active profiles, @Profile annotation, ... Introduction to Spring Profiles – Learn Spring and Spring Boot Profiles to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found