2

I have some integration tests that create/delete entries in my database. The problem is if I run the tests with

spring.profiles.active=prod

my production database gets deleted (as the tests clear the database). Is there any way to prevent tests from running on this specific spring profile?

I have seen this thread: How to prevent running tests when specific spring profile is active? but there was no useful answer.

Thank you

AFortunato
  • 181
  • 1
  • 10
  • Does this help? https://stackoverflow.com/questions/39048171/how-to-run-turn-off-selective-tests-based-on-profiles-in-spring-boot – aksappy Mar 25 '21 at 17:28
  • @aksappy, unfortunately, no, none of those answers solves the problem, although the question is the same, how to disable a test class from running by default. Thank you anyway – AFortunato Mar 25 '21 at 22:58
  • are you not running with `@Transactional`? this annotation role back everything after tests – Amir Azizkhani Feb 28 '22 at 06:04

3 Answers3

2

I was able to resolve the problem following this comment: https://stackoverflow.com/a/32892291/8679100. The solution is not perfect, as I need to verify if the prod profile is active in @BeforeAll and @AfterAll, but it does work. Furthermore, System.getProperty("spring.profiles.active", "");didn't actually work, butArrays.stream(environment.getActiveProfiles()).anyMatch(env -> (env.equalsIgnoreCase("prod")))``` did

AFortunato
  • 181
  • 1
  • 10
1

There could be multiple solution to your problem.

  1. Using in-memory databases like H2 for Sql and flapdoodle for no-sql for running tests. **preferred way
  2. Create a separate properties file with clone of spring properties. Just change the database properties/spring profile or other things. Use this properties file with @testpropertysource on test class.
  3. Use @dirtiescontext on tests to create/delete impacted rows only.
  4. Another thing you can do is create stud classes your database layer to mock operations.
Sachin
  • 176
  • 1
  • 11
  • I am using a different database for different profiles, the tests will not run by default on the production database. This is a precaution against possible distractions – AFortunato Mar 25 '21 at 22:59
0

You can use @IfProfileValue annotation to disable the test. it's not directly depend on the spring profile, but you can easily use config file to set the value you want based on the spring profile.

Having said that - it sounds very risky to run tests that delete entries from the db (or any other db transaction) on production DB. I support what @Sachin suggested above - run your test on tests environment, not production

Nir Levy
  • 12,750
  • 3
  • 21
  • 38
  • I am only running the tests on the test environment, but I want to prevent situations such as running ```mvn test``` when ```spring.profiles.active=prod``` by accident. I'm not sure how to use @IfProfileValue. I'm tried to do ```@IfProfileValue(name = "spring.profiles.active", value = "prod")```, but it still runs when ```spring.profiles.active``` is "dev". Could you please provide an example? – AFortunato Mar 25 '21 at 23:01