环境 getProperty("SomeValue") 值在 Spring 测试和 Mockito

我正在为控制器类编写 JUnit。我正在使用@PropertySource("classpath:webmvc_test.properties")和Environment反对从属性文件中读取值。在调用getProperty()方法获取null价值。属性文件webmvc_test.properties在类路径下。


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

    }


    }

}


慕容708150
浏览 87回答 1
1回答

RISEBY

我遇到了同样的问题,当我为它寻找解决方案时,我发现了这篇文章@Autowired + PowerMock:修复一些 Spring Framework 的误用/滥用似乎 powermock 和 spring 之间存在设计问题,无法@Autowire在测试类中正常工作, 所以不要使用@Autowireuse@Mock并期望返回值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 {&nbsp; &nbsp; @Mock&nbsp; &nbsp; private FamilyService familyService;&nbsp; &nbsp; @InjectMocks&nbsp; &nbsp; private WelcomeController welcomeController;&nbsp; &nbsp; @Mock&nbsp; &nbsp; private Environment environment;&nbsp; &nbsp; private MockMvc mockMvc;&nbsp; &nbsp; @Before&nbsp; &nbsp; public void setup() {&nbsp; &nbsp; &nbsp; &nbsp; MockitoAnnotations.initMocks(this);&nbsp; &nbsp; &nbsp; &nbsp; this.mockMvc = MockMvcBuilders.standaloneSetup(welcomeController).build();&nbsp; &nbsp; &nbsp; &nbsp; when(environment.getProperty("familyId")).thenReturn("1");&nbsp; &nbsp; &nbsp; &nbsp; when(environment.getProperty("familyMemberName")).thenReturn("Ramachandrappa Kalavakuri");&nbsp; &nbsp; &nbsp; &nbsp; when(environment.getProperty("familyMemberAge")).thenReturn("36");&nbsp; &nbsp; &nbsp; &nbsp; when(environment.getProperty("familyAddress")).thenReturn("Flat no: 305, 2nd Floor, Prakasa Pride Apartments, Opp To J.P.Morgan, Kadubesinahalli, Bangalore - 560087");&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; @Test&nbsp; &nbsp; public void welcomePage() throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; FamilyVO allFamilyMembers = getAllFamilyMembers();&nbsp; &nbsp; &nbsp; &nbsp; when(familyService.getAllFamilyMembers()).thenReturn(allFamilyMembers);&nbsp; &nbsp; &nbsp; &nbsp; mockMvc.perform(get("/")).andExpect(status().isOk()).andExpect(view().name("Index"));&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @return&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private FamilyVO getAllFamilyMembers() {&nbsp; &nbsp; &nbsp; &nbsp; FamilyVO allFamilyMembers = new FamilyVO();&nbsp; &nbsp; &nbsp; &nbsp; FamilyVO familyVO = new FamilyVO();&nbsp; &nbsp; &nbsp; &nbsp; familyVO.setFamilyId(Integer.parseInt(environment.getProperty("familyId")));&nbsp; &nbsp; &nbsp; &nbsp; familyVO.setFamilyMemberName(environment.getProperty("familyMemberName"));&nbsp; &nbsp; &nbsp; &nbsp; familyVO.setFamilyMemberAge(Integer.parseInt(environment.getProperty("familyMemberAge")));&nbsp; &nbsp; &nbsp; &nbsp; FamilyAddress familyAddress = new FamilyAddress();&nbsp; &nbsp; &nbsp; &nbsp; familyAddress.setAddress(environment.getProperty("familyAddress"));&nbsp; &nbsp; &nbsp; &nbsp; familyVO.setFamilyAddress(familyAddress);&nbsp; &nbsp; &nbsp; &nbsp; List<FamilyVO> familyVOs = new ArrayList<FamilyVO>();&nbsp; &nbsp; &nbsp; &nbsp; familyVOs.add(familyVO);&nbsp; &nbsp; &nbsp; &nbsp; allFamilyMembers.setFamilyVOs(familyVOs);&nbsp; &nbsp; &nbsp; &nbsp; return allFamilyMembers;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java