简体   繁体   English

在Spring MVC中模拟属性文件以进行Junit测试

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

I am trying to write a Junit test case for my repository class which is uses a property file. 我正在尝试为使用属性文件的存储库类编写Junit测试用例。 I am using spring 3.2.4. 我正在使用Spring 3.2.4。 problem is i am not able to. 问题是我不能。 Below is my code snippet : 以下是我的代码段:

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

and below is my Test file : 以下是我的测试文件:

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

}

But this is failing saying expected was zero but it should be 4. Am i missing something. 但这不能说期望值是零,但应该是4。我错过了什么吗? any good explains on mocking property file in Junit testing is most welcome. 欢迎在Junit测试中提供有关模拟属性文件的任何很好的解释。

The annotation will need mocking 注释将需要嘲笑

there appears to be a good explaination here http://blog.jamesdbloom.com/UsingPropertySourceAndEnvironment.html 这里似乎有一个很好的解释http://blog.jamesdbloom.com/UsingPropertySourceAndEnvironment.html

If you use a standard build tool like maven, you have different classpaths for testing and production. 如果使用诸如maven之类的标准构建工具,则将有不同的类路径用于测试和生产。 Just place a test-specific properties file in src/test/resources, it will take precedence over the one in src/main/resources 只需将特定于测试的属性文件放在src / test / resources中,它将优先于src / main / resources中的一个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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