vendredi 23 octobre 2020

@EnableTransactionManagement breaks my tests

So, I have a Spring Boot app. Recently I added an aspect I would like to apply after @Transaction aspect handler. So on my main app class I added @EnableTransactionManagement with lowering the order for transactions.

@SpringBootApplication
@EntityScan(basePackageClasses = { Application.class, Jsr310JpaConverters.class })
@EnableAutoConfiguration(exclude = RepositoryRestMvcAutoConfiguration.class)
@EnableSpringDataWebSupport
@EnableScheduling
// Need to lower the transaction aspect priority in order to SyncAspect have a higher prio
// The lower the number the higher the priority, defaults to HIGHEST_PRECEDENCE = -2147483648
@EnableTransactionManagement(order = Ordered.HIGHEST_PRECEDENCE + 2)
public class Application extends SpringBootServletInitializer {

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

The order here does not matter (the test fails even when I just add @EnableTransactionManagement without any additional config) but after adding this annotation one of our tests failed. It is a @WebMVCTest and now it returns 404 for all the requests made in the test. Here is the test class declared.

@RunWith(SpringRunner.class)
@WebMvcTest(controllers = OptionMenuController.class)
@WithMockUser(roles = { "ENTERPRISE_USER" })
@ActiveProfiles(profiles = {"develop"})
public class OptionMenuControllerTest extends AbstractFullFeatureSetControllerTest<OptionMenu> {
...


@Import(DummySecurityConfiguration.class)
public abstract class AbstractFullFeatureSetControllerTest<EntityType extends Identifiable>
    extends AbstractControllerTest<EntityType>
...


@TestConfiguration
public class DummySecurityConfiguration {

    @Bean(name = "store_based_auth")
    public UserDetailsService storeBasedAuthService() {
        return Mockito.mock(UserDetailsService.class);
    }

    @Bean(name = "webui")
    public UserDetailsService webUiService() {
        return Mockito.mock(UserDetailsService.class);
    }

    @Bean(name = "integration_api_auth")
    public UserDetailsService integrationApiAuthService() {
        return Mockito.mock(UserDetailsService.class);
    }

    @Bean(name = "onboarding_api_auth")
    public UserDetailsService onboardingApiAuthService() {
        return Mockito.mock(UserDetailsService.class);
    }

    @Bean
    public JwtService jwtService() {
        return Mockito.mock(JwtService.class);
    }
}

I have no idea why it happens. It looks really strange. But my guess is it has something to do with this @WithMockUser annotation. Maybe when I added @EnableTransactionManagement on a main class it reconfigured something in a wrong way that a dummy user is not found anymore that's why I get 404 for all the tests in test class. Any ideas or hints?

While debugging I found that the controller method is not ever run.

Aucun commentaire:

Enregistrer un commentaire