运行junit测试时排除某些bean

时间:2018-10-15 13:11:02

标签: java spring junit

因此,我有以下两个Java测试类:

package com.company.alfresco;

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

    @Autowired
    private CmisDao cmisDao;

    @Autowired
    private AlfrescoFileTreeAPI alfrescoFileTreeAPI;

    ...
}

package com.company.facade;

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

    @Autowired
    private FacadeInitializer facadeInitializer;

    ...
}

我还有两个单独的主要方法类,每个测试类都使用一个:

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);
    }
}

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);
    }
}

我想运行第一个测试(BuildFileTreeTest),而不使用另一个测试正在使用的bean(FacadeInitializer)。

由于某种原因,使用此程序包设置,当我运行BuildFileTreeTest测试类时,FacadeInitializer bean仍在应用程序上下文中使用,尽管它存在于“ facade”程序包中,我没有将其包含在@ComponentScan spring main方法类的BuildFileTreeApplication批注中。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您可以排除具有类型,注释等的bean。

SessionFactory