Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在SpringMVC中为Junit测试模拟属性文件_Java_Spring Mvc - Fatal编程技术网

Java 在SpringMVC中为Junit测试模拟属性文件

Java 在SpringMVC中为Junit测试模拟属性文件,java,spring-mvc,Java,Spring Mvc,我正在尝试为使用属性文件的存储库类编写Junit测试用例。我使用的是spring 3.2.4。问题是我不能。下面是我的代码片段: @Configuration @PropertySource("classpath:properties/filterTemplate.properties") public class FilterTemplateRepository { @Resource private Environment env; @Bean publ

我正在尝试为使用属性文件的存储库类编写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测试中模拟属性文件的任何好的解释。

注释将需要模拟


这里似乎有一个很好的解释

如果您使用像maven这样的标准构建工具,那么您有不同的类路径用于测试和生产。只需在src/test/resources中放置一个特定于测试的属性文件,它将优先于src/main/resources中的属性文件