Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to run/turn off selective tests based on profiles in spring boot

I have a spring-boot application for which I am writing IT tests.

The data for the tests comes from application-dev.properties when I activate dev profile

Here is what I have for tests:

@RunWith(SpringRunner.class)
@SpringBootTest
@WebAppConfiguration
public class ApplicationTests {

    @Autowired
    Environment env;

    @Test
    public void contextLoads() {
        System.out.println(Arrays.toString((env.getActiveProfiles())));

    }

}

ServiceITTest

public class ServiceITTest extends ApplicationTests {


     @value
     String username;

     @value
     String address;

     @Autowired
     MyService myService;


      @Test
      public void check_for_valid_username_address(){
            myService.validate(username,address);
      }
}

I want the above test to run only when I set the profile of "dev","qa". by default, it should not run.

Is it possible to get that fine control in spring boot testing?

like image 689
brain storm Avatar asked Aug 19 '16 21:08

brain storm


3 Answers

You would want to use the @IfProfileValue annotation. Unfortunately it doesn't work directly on the active profiles but it can read a property so if you only define a specific property within the profiles that you want to run the test on then you can use that annotation on that specific property.

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/integration-testing.html#integration-testing-annotations-junit

like image 106
Shawn Clark Avatar answered Oct 22 '22 17:10

Shawn Clark


It works also with active profiles - there is a property value containing active profiles:

Test only active with specific profile:

@IfProfileValue(name = "spring.profiles.active", values = {"specific"})

Since i have tests that should NOT run if specific profile is active i added this to those tests:

@ActiveProfiles(profiles = {"default"})

It does not work with @IfProfileValue and "default" and i also didn't found any "run if specific profile is not active.

like image 24
dermoritz Avatar answered Oct 22 '22 16:10

dermoritz


In Spring you can also use the @DisabledIf annotation. It allows for specifying a Spring Expression Language expression. See this blog post for examples.

JUnit 5 also has:

  • @DisabledIfEnvironmentVariable
  • @DisabledIfSystemProperty
like image 4
Mark Lagendijk Avatar answered Oct 22 '22 15:10

Mark Lagendijk