Mock 编写一个Spring Controller 测试类

本文介绍了一个使用Spring MVC框架实现的简单Controller类及Repository接口示例,其中包括了Controller如何通过Repository获取数据并返回视图的过程。同时,提供了一个用于验证Controller功能的测试类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

###Controller

package com.brainysoon.zkeps.web;

import com.brainysoon.zkeps.data.KepsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Created by ken on 16-10-13.
 */
@Controller
@RequestMapping("/keps")
public class KepsController {

    private KepsRepository kepsRepository;

    @Autowired
    public void setKepsRepository(KepsRepository kepsRepository) {

        this.kepsRepository = kepsRepository;
    }

    @RequestMapping(method = RequestMethod.GET)
    public String keps(@RequestParam(value = "max", defaultValue = "1") long max,
                       @RequestParam(value = "count", defaultValue = "20") long count,
                       Model model) {

        model.addAttribute("keps", kepsRepository.findKeps(max, count));

        return "keps";
    }
}

###Repository 接口

package com.brainysoon.zkeps.data;

import com.brainysoon.zkeps.bean.Kep;

import java.util.List;

/**
 * Created by ken on 16-10-13.
 */
public interface KepsRepository {

    //获得一定数量的帖子
    List<Kep> findKeps(long max, long count);

    //得到一个帖子
    Kep findOne(long kepId);
}

###测试类 TestKepsController

package com.brainysoon.zkeps.web;

import com.brainysoon.zkeps.bean.Kep;
import com.brainysoon.zkeps.data.KepsRepository;
import com.brainysoon.zkeps.data.MockKepsRepository;
import org.junit.Test;
import org.springframework.test.web.servlet.MockMvc;

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

import static org.mockito.Mockito.mock;
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.model;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;

/**
 * Created by ken on 16-10-13.
 */
public class TestKepsController {
    
    @Test
    public void testKeps() throws Exception {

        KepsController kepsController = new KepsController();

        List<Kep> keps = createKeps(3, 20);

        KepsRepository kepsRepository = mock(KepsRepository.class);     //MockRepository

        when(kepsRepository.findKeps(3, 20)).thenReturn(keps);

        kepsController.setKepsRepository(kepsRepository);

        MockMvc mockMvc = standaloneSetup(kepsController)               //MockSpringMvc
                .setViewResolvers(new WebConfig().viewResolver()).build();

        mockMvc.perform(get("/keps?&max=3&count=20")).andExpect(view().name("keps"))    //SpringExpect
                .andExpect(model().attributeExists("keps"))
                .andExpect(model().attribute("keps", keps));
    }

    public List<Kep> createKeps(long max, long count) {

        List<Kep> keps = new ArrayList<>();

        for (long i = max; i <= max + count; i++) {

            keps.add(new Kep(i, "第" + i + "条帖子", new Date(), "用户:" + i));
        }

        return keps;
    }
}

###GitHub 项目地址

Migrated to https://sheltonsuen.github.io

转载于:https://my.oschina.net/brainysoon/blog/758764

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值