Search code examples
javaspring-boot-testspring-profiles

@SpringBootTest Toggable webEnvironment?


I want to toggle the webEnvironment config inside SpringBootTest to support running the tests in a pipeline (where the tests needs to be able to boot the server themselves) and locally (where I want to use a standalone server for faster tests.

Figured having a Profile which I could set would be a good solution to it but SpringBootTest seems to flat out ignore whatever profile I attach at the same level, is it simply too early in Spring's lifecycle for it to pick up profiles? Is there a better way to do this?

@Profile("myProfile")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) // Starts regardless of what @Profile is
public class MyClass{
   ...
}

E: Related question: SpringBootTest enable/disable webEnvironment based on input

E2: After some discussion down below I'm ditching the remote mode, not worth the hassle.


Solution

  • I was wondering why do you even need to rely on spring here? JUnit already has @EnabledIf annotation (documentation), so you can use it to not even attempt to run the test.

    In my understanding it's even better, because this will work for all the tests that might even not run spring (unit tests for example). Also it should be better from the performance endpoint (you don't try to even run the Application Context, find/register beans, etc.)