2

I am writing JUnits for controller classes. I am using @PropertySource("classpath:webmvc_test.properties") and Environment object to read the values from properties file. On calling getProperty() method getting null value. The property file webmvc_test.properties is under the class path.

TestClass.java:

package com.kalavakuri.webmvc.web.controller;

import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;

import java.util.ArrayList;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import com.kalavakuri.webmvc.business.service.FamilyService;
import com.kalavakuri.webmvc.business.valueobject.FamilyAddress;
import com.kalavakuri.webmvc.business.valueobject.FamilyVO;
import com.kalavakuri.webmvc.init.ApplicationInitializer;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { ApplicationInitializer.class })
@PropertySource("classpath:webmvc_test.properties")
public class WelcomeControllerTest {

    @Mock
    private FamilyService familyService;

    @InjectMocks
    private WelcomeController welcomeController;

    @Autowired
    private Environment environment;

    private MockMvc mockMvc;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
        this.mockMvc = MockMvcBuilders.standaloneSetup(welcomeController).build();
    }

    @Test
    public void welcomePage() throws Exception {

        FamilyVO allFamilyMembers = getAllFamilyMembers();

        when(familyService.getAllFamilyMembers()).thenReturn(allFamilyMembers);
        mockMvc.perform(get("/")).andExpect(status().isOk()).andExpect(view().name("Index"));
    }

    /**
     * @return
     */
    private FamilyVO getAllFamilyMembers() {
        FamilyVO allFamilyMembers = new FamilyVO();
        FamilyVO familyVO = new FamilyVO();
        familyVO.setFamilyId(Integer.parseInt(environment.getProperty("familyId")));
        familyVO.setFamilyMemberName(environment.getProperty("familyMemberName"));
        familyVO.setFamilyMemberAge(Integer.parseInt(environment.getProperty("familyMemberAge")));

        FamilyAddress familyAddress = new FamilyAddress();
        familyAddress.setAddress(environment.getProperty("familyAddress"));
        familyVO.setFamilyAddress(familyAddress);

        List<FamilyVO> familyVOs = new ArrayList<FamilyVO>();
        familyVOs.add(familyVO);

        allFamilyMembers.setFamilyVOs(familyVOs);
        return allFamilyMembers;
    }
}


webmvc_test.properties:

familyId=1
familyMemberName=Ramachandrappa Kalavakuri
familyMemberAge=36
familyAddress=Flat no: 305, 2nd Floor, Prakasa Pride Apartments, Opp To J.P.Morgan, Kadubesinahalli, Bangalore - 560087
informatik01
  • 16,038
  • 10
  • 74
  • 104
user995656
  • 123
  • 1
  • 14
  • how did you run test ? possible resources was not presented in classpath – borino May 16 '19 at 11:48
  • I have mentioned that, webmvc_test.properties are classpath - Its under src/test/resources. – user995656 May 16 '19 at 12:40
  • Just to be sure, if your file located in right place in the IDE, it does not mean 100% that in executing time it presents in classpath, because it depends on how do you run your tests. – borino May 17 '19 at 04:25

1 Answers1

0

I had the same problem and when I searched for a solution for it I found this article @Autowired + PowerMock: Fixing Some Spring Framework Misuse/Abuse it seems that there is a design problem between powermock and spring that prevent @Autowire from working correctly inside test classes, So instead of using @Autowire use @Mock and expect the returned values

package com.kalavakuri.webmvc.web.controller;

import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;

import java.util.ArrayList;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import com.kalavakuri.webmvc.business.service.FamilyService;
import com.kalavakuri.webmvc.business.valueobject.FamilyAddress;
import com.kalavakuri.webmvc.business.valueobject.FamilyVO;
import com.kalavakuri.webmvc.init.ApplicationInitializer;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { ApplicationInitializer.class })
@PropertySource("classpath:webmvc_test.properties")
public class WelcomeControllerTest {

    @Mock
    private FamilyService familyService;

    @InjectMocks
    private WelcomeController welcomeController;

    @Mock
    private Environment environment;

    private MockMvc mockMvc;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
        this.mockMvc = MockMvcBuilders.standaloneSetup(welcomeController).build();

        when(environment.getProperty("familyId")).thenReturn("1");
        when(environment.getProperty("familyMemberName")).thenReturn("Ramachandrappa Kalavakuri");
        when(environment.getProperty("familyMemberAge")).thenReturn("36");
        when(environment.getProperty("familyAddress")).thenReturn("Flat no: 305, 2nd Floor, Prakasa Pride Apartments, Opp To J.P.Morgan, Kadubesinahalli, Bangalore - 560087"); 
    }

    @Test
    public void welcomePage() throws Exception {

        FamilyVO allFamilyMembers = getAllFamilyMembers();

        when(familyService.getAllFamilyMembers()).thenReturn(allFamilyMembers);
        mockMvc.perform(get("/")).andExpect(status().isOk()).andExpect(view().name("Index"));
    }

    /**
     * @return
     */
    private FamilyVO getAllFamilyMembers() {
        FamilyVO allFamilyMembers = new FamilyVO();
        FamilyVO familyVO = new FamilyVO();
        familyVO.setFamilyId(Integer.parseInt(environment.getProperty("familyId")));
        familyVO.setFamilyMemberName(environment.getProperty("familyMemberName"));
        familyVO.setFamilyMemberAge(Integer.parseInt(environment.getProperty("familyMemberAge")));

        FamilyAddress familyAddress = new FamilyAddress();
        familyAddress.setAddress(environment.getProperty("familyAddress"));
        familyVO.setFamilyAddress(familyAddress);

        List<FamilyVO> familyVOs = new ArrayList<FamilyVO>();
        familyVOs.add(familyVO);

        allFamilyMembers.setFamilyVOs(familyVOs);
        return allFamilyMembers;
    }
}
Amgad Hanafy
  • 537
  • 5
  • 15