Search code examples
javaspring-bootspring-boot-test

SpringBootTest exclude packages from being scanned


I want to exclude some packages from being scanned when using spring @SpringBootTest in the same way it is done with @ComponentScan. Is there something like

@SpringBootTest(excludeFilters [email protected](
            type = FilterType.REGEX,
            pattern = "package\\.\\.to\\.Exclude.*"))

Solution

  • It seems that the best workaround on this is to create a class annotated with the @SpringBootApplication and configure the scanning configuration there

    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication(scanBasePackageClasses ={TestConfiguration.class})
    public class TestApp {
    }
    

    Then your unit test you should specify the test class created previously

    @SpringBootTest(classes = TestApp.class)
    public class UnitTestClass {
    }