第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

環(huán)境 getProperty("SomeValue") 值在 Spring 測(cè)試和 Mockito

環(huán)境 getProperty("SomeValue") 值在 Spring 測(cè)試和 Mockito

慕容708150 2022-11-30 10:10:03
我正在為控制器類編寫 JUnit。我正在使用@PropertySource("classpath:webmvc_test.properties")和Environment反對(duì)從屬性文件中讀取值。在調(diào)用getProperty()方法獲取null價(jià)值。屬性文件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();    }    }}
查看完整描述

1 回答

?
RISEBY

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超5個(gè)贊

我遇到了同樣的問題,當(dāng)我為它尋找解決方案時(shí),我發(fā)現(xiàn)了這篇文章@Autowired + PowerMock:修復(fù)一些 Spring Framework 的誤用/濫用似乎 powermock 和 spring 之間存在設(shè)計(jì)問題,無法@Autowire在測(cè)試類中正常工作, 所以不要使用@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 {


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

    }

}


查看完整回答
反對(duì) 回復(fù) 2022-11-30
  • 1 回答
  • 0 關(guān)注
  • 125 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)