How can I load a @Profile
component in a spring-test
, without having to explicit launch that profile as startup profile?
@Configuration
@Profile("dev1")
public class MvcInterceptor extends WebMvcConfigurerAdapter {
//adding a custom interceptor
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MappedInterceptor("/customer", new CustomerInterceptor()));
super.addInterceptors(registry);
}
}
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
//@ActiveProfiles("dev1") //I don't want spring to load the full profile chain
public class SpringTest {
@Autowired
protected MockMvc mvc;
@Test
public void test() {
mvc.perform(get(...))...;
}
}
Question: how can I load the MvcInterceptor
without using @ActiveProfiles("dev1")
on the testclass?
Because, dev1
profile would imply many more resources being set up, which I won't need for that test. I just want to load the MvcInterceptor
, but without having to launch the full profile.
Impossible?
It works by initializing the a Bean
in a static inner @TestConfiguration
class.
public class SpringTest {
@TestConfiguration
public static class TestConfig {
@Bean
public MvcInterceptor interceptor() {
return new MvcInterceptor();
}
}
}
Though MvcInterceptor
is @Profile
depending, it will be wired for the test.