简体   繁体   中英

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. I am using 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. any good explains on mocking property file in Junit testing is most welcome.

The annotation will need mocking

there appears to be a good explaination here http://blog.jamesdbloom.com/UsingPropertySourceAndEnvironment.html

If you use a standard build tool like maven, you have different classpaths for testing and production. Just place a test-specific properties file in src/test/resources, it will take precedence over the one in src/main/resources

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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