簡體   English   中英

在Spring MVC中模擬屬性文件以進行Junit測試

[英]Mocking property file in spring mvc for Junit testing

我正在嘗試為使用屬性文件的存儲庫類編寫Junit測試用例。 我正在使用Spring 3.2.4。 問題是我不能。 以下是我的代碼段:

@Configuration
@PropertySource("classpath:properties/filterTemplate.properties")
public class FilterTemplateRepository  {

    @Resource
    private Environment env;

    @Bean
    public Map<String,List<FilterTemplate>> getBean()
    {
        Map<String,List<FilterTemplate>> filterTemplateMap=new HashMap<String,List<FilterTemplate>>();
        // basic code

        return filterTemplateMap;

    }

    public List<FilterTemplate> getModuleData(String moduleName)
    {

        return getBean().get(moduleName);
    }
}

以下是我的測試文件:

public class FilterTemplateRepositoryTest{

    private FilterTemplateRepository filterTemplateRepository;

    @Before
    public void setUp() throws Exception {
        filterTemplateRepository= Mockito.mock(FilterTemplateRepository.class);;
    }

    @Test
    public void testgetModuleData() {
        String moduleName="dashboard";
        List<FilterTemplate> filterTemplatelist=new ArrayList<FilterTemplate>();;
        filterTemplatelist=filterTemplateRepository.getModuleData(moduleName);
        assertEquals(4,filterTemplatelist.size());
    }

}

但這不能說期望值是零,但應該是4。我錯過了什么嗎? 歡迎在Junit測試中提供有關模擬屬性文件的任何很好的解釋。

注釋將需要嘲笑

這里似乎有一個很好的解釋http://blog.jamesdbloom.com/UsingPropertySourceAndEnvironment.html

如果使用諸如maven之類的標准構建工具,則將有不同的類路徑用於測試和生產。 只需將特定於測試的屬性文件放在src / test / resources中,它將優先於src / main / resources中的一個

暫無
暫無

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

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