1, 如果我们只是想要mock某一组件可以这样:

  • 依赖pom.xml

    4.0.0
    com.zws
    springmvc-test
    0.0.1-SNAPSHOT

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.4.RELEASE
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            org.springframework.boot
            spring-boot-test-autoconfigure
        

        
            com.alibaba
            fastjson
            1.2.49
        
    
  • Application启动类
package com.zws.mvntest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
  • UserService
package com.zws.mvntest.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zws.mvntest.bean.User;
import com.zws.mvntest.dao.UserDao;

@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    public User getUserById(Long id) {
        return userDao.getUserById(id);
    }
}
  • UserDao
package com.zws.mvntest.dao;

import com.zws.mvntest.bean.User;

public interface UserDao {

    User getUserById(Long id);
}
  • 测试类UserServiceTest
package com.zws.mvntest;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.BDDMockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;

import com.zws.mvntest.bean.User;
import com.zws.mvntest.dao.UserDao;
import com.zws.mvntest.service.UserService;

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserServiceTest {

    @MockBean
    private UserDao userDao;

    @Autowired
    private UserService userService;

    @Test
    public void getUserById() throws Exception {
        Long id = 1L;
        User user = new User();
        user.setName("张三0000");
        user.setGender("男");

        BDDMockito.given(this.userDao.getUserById(id)).willReturn(user);

        User u = this.userService.getUserById(1L);
        System.out.println(u);

        Assert.assertNotNull(u);
        Assert.assertEquals(u.getName(), user.getName());
        Assert.assertEquals(u.getGender(), user.getGender());
    }

}

这里UserDao没有实现所以mock,用到了@MockBean注解。

mock mvc

  • UserController
package com.zws.mvntest.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.zws.mvntest.bean.User;
import com.zws.mvntest.service.UserService;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/user")
    public String getUser() {
        User user = userService.getUserById(1L);

        return JSON.toJSONString(user);
    }
}
  • 测试类UserControllerTest
package com.zws.mvntest;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.BDDMockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import com.zws.mvntest.bean.User;
import com.zws.mvntest.dao.UserDao;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class UserControllerTest {

    @Autowired
    private MockMvc mvc;

    @MockBean
    private UserDao userDao;

    @Test
    public void exampleTest() throws Exception {
        Long id = 1L;

        BDDMockito.given(this.userDao.getUserById(id)).willReturn(new User("张三", "男1"));

        RequestBuilder builder = MockMvcRequestBuilders.get("/user").accept(MediaType.TEXT_HTML);
        ResultActions result = this.mvc.perform(builder);

        result.andExpect(MockMvcResultMatchers.status().isOk());

        String context = "{\"gender\":\"男1\",\"name\":\"张三\"}";
        ResultMatcher matcher = MockMvcResultMatchers.content().string(context);
        result.andExpect(matcher);
    }
}

参考Spring Boot官方文档,记录下这个mock方式。