簡體   English   中英

運行junit測試時排除某些bean

[英]Exclude certain beans when running a junit test

因此,我有以下兩個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注釋,位於BuildFileTreeApplication spring主方法類中。 有任何想法嗎 ?

您可以排除具有類型,注釋等的bean。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM