简体   繁体   中英

Exclude certain beans when running a junit test

So, I have the two following java test classes:

package com.company.alfresco;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = BuildFileTreeApplication.class)
@Slf4j
public class BuildFileTreeTest {

    @Autowired
    private CmisDao cmisDao;

    @Autowired
    private AlfrescoFileTreeAPI alfrescoFileTreeAPI;

    ...
}

and

package com.company.facade;

@SpringBootTest
@Slf4j
public class EntryPointTest extends AbstractTestNGSpringContextTests{
    private Map<String, String> documentMetadata;

    @Autowired
    private FacadeInitializer facadeInitializer;

    ...
}

I also have two separate main method classes, one used by each test class:

package com.company;

@SpringBootApplication
@ComponentScan(basePackages = {"com.company", "com.company.ssh", "com.company.camel",
"com.company.cmis.services", "com.company.facade"})
public class AlfrescoApiApplication {
    public static void main(String[] args){
        SpringApplication.run(AlfrescoApiApplication.class, args);
    }
}

and

package com.company;

@SpringBootApplication
@ComponentScan(basePackages = {"com.company.alfresco", "com.company.models",
    "com.company.config"})
public class BuildFileTreeApplication {
    public static void main(String[] args){
        SpringApplication.run(BuildFileTreeApplication.class, args);
    }
}

I want to run the first test (the BuildFileTreeTest ), without using the bean that the other test is using ( FacadeInitializer ).

For some reason, using this package setup, when I am running the BuildFileTreeTest test class, the FacadeInitializer bean is still used in the application context, altough it is present in the "facade" package, which I didn't include in the @ComponentScan annotation, within the BuildFileTreeApplication spring main method class. Any ideas ?

You can exclude beans with types, annotations etc.

@SpringBootApplication
@ComponentScan(basePackages = {"com.company.alfresco", "com.company.models", "com.company.config"},
        excludeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {FacadeInitializer.class})})
public class BuildFileTreeApplication {
    public static void main(String[] args) {
        SpringApplication.run(BuildFileTreeApplication.class, args);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM