5

Given a test class like:

@WebMvcTest
@RunWith(SpringRunner.class)
@SpringBootTest(properties = "spring.profiles.active=test")
public class MyControllerTest  {
... some tests
}

I get the error:

java.lang.IllegalStateException: Configuration error: found multiple declarations of @BootstrapWith for test class [com.example.MyControllerTest]: [@org.springframework.test.context.BootstrapWith(value=class org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTestContextBootstrapper), @org.springframework.test.context.BootstrapWith(value=class org.springframework.boot.test.context.SpringBootTestContextBootstrapper)]

The desired goal is that I'm just running a controller test and thus for test performance reasons do not want to set up the whole context - I just want the "web layer".

I can remove the @SpringBootTest(properties = "spring.profiles.active=test") line - however, now I've not activated the test profile, which may customise the web context in some way via properties, e.g. jackson customisations that will no longer be applied. Is there a way to get a "web layer" only test and still activate a spring profile?

My environment is java version "10.0.2" 2018-07-17, spring boot 1.5.16.RELEASE

David
  • 7,652
  • 21
  • 60
  • 98
  • Probably [here](https://spring.io/guides/gs/testing-web/) are couple of options if you haven't known earlier – Amit Phaltankar Oct 16 '18 at 22:58
  • It's a nice article showing various test approaches, but I don't see anything related to this. – David Oct 16 '18 at 23:01
  • What do you mean by `activate a spring profile`. Which spring profile you want to activate ? – Amit Phaltankar Oct 16 '18 at 23:07
  • A custom one. In this case it's called `test`. i.e. there is an `application-test.properties` file on the classpath. This file can do things like set jackson serialisation options which can change the behaviour of the application. With the profile activated the outcome can be different to when it is not activated, thus I want to activate it in the test. – David Oct 16 '18 at 23:11

2 Answers2

8

To set active profile, you can use @ActiveProfiles, like this

@WebMvcTest
@RunWith(SpringRunner.class)
@ActiveProfiles("test")
public class MyControllerTest  {

then you can you application-test yml or properties in test resources.

Amit Phaltankar
  • 3,341
  • 2
  • 20
  • 37
  • 1
    If you have some Profile based Component, that would not be created. For example I have a component created in DEV and test profile and when I run ControllerTest with @WebMvcTest and @ActiveProfiles("test"), the applicationContext doesn't create that component. – beinghuman Jan 12 '21 at 14:00
0

Additionally, if you are getting active profile in programmatic way like this:

String environment = System.getProperty("spring.profiles.active");

Then you can set this property in the static block in your test class:

@WebMvcTest
public class MyControllerTest  {
    static {
        System.setProperty("spring.profiles.active", "foo");
    }

}
monstereo
  • 830
  • 11
  • 31