vendredi 14 février 2020

Test slice with @WebMvcTest is loading a substantial amount of controllers unrelated with the target

I have a spring boot application yielding numerous controllers and my goal is to create an integration test for a specific one. I read that we can achieve a test slice with the @WebMvcTest annotation that loads only what is necessary to deploy the target controller, is this assumption correct? Here is my test:

@RunWith(SpringRunner.class)
@WebMvcTest(
        controllers = {DummyController.class},
)
public class DummyControllerIT {

    @Autowired
    private MockMvc mockMvc;

...

Unfortunately the execution attempts to deploy other controllers/services/repositories that have no relation to the target Controller, which forces me use @MockBean on each of them. I was under the impression that @WebMvcTest would spare me from having an extensive listing of declared controllers/services/repositories with the @MockBean annotation, am I wrong?

If I misinterpreted this and I am expected to use @MockBean on unrelated parts of the application, then why is it better to use @WebMvcTest instead of @SpringBootTest? On the other hand, if I interpreted it correctly what am I missing?


Not sure if it is related but this is my initialiser:

@ComponentScan(scopedProxy = ScopedProxyMode.INTERFACES)
@SpringBootApplication
@EnableTransactionManagement
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableScheduling
@EnableCaching
@EnableJpaAuditing
@EnableJpaRepositories(repositoryFactoryBeanClass = EnversRevisionRepositoryFactoryBean.class)
public class Application extends SpringBootServletInitializer {

    @Autowired
    private Environment env;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    @Bean
    @Primary
    public TaskExecutor threadPoolTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(Integer.parseInt(Objects.requireNonNull(env.getProperty("coreThreadPoolSize"))));
        executor.setMaxPoolSize(Integer.parseInt(Objects.requireNonNull(env.getProperty("maxThreadPoolSize"))));
        executor.initialize();
        return executor;
    }

}

Thank you for your help.

Aucun commentaire:

Enregistrer un commentaire