Fork me on GitHub

Mockito 简单使用

有一个月没写博客了,以后再忙也要抽时间写啊。

目的

正常情况下,如果要对 UserService 中方法的测试,那么其依赖的 UserDao 也要可以调通,但是,UserDao 可能并不是直接到 DB 的,也可能到各种存储介质中,那么问题就复杂了,我们的目的仅仅是要测试 UserService 中的业务处理逻辑,UserDao 返回的结果并不重要

这种情况下,如果我们能模拟一个 UserDao 返回的结果来供 UserService 处理,就太好了

 

下面通过 Mockito 就能很简单的实现

实现

项目结构

User:

package com.zhengbin.service.test;

/**
 * Created by ZhengBin on 2017/6/30.
 */
public class User {
    private Integer id;
    private String name;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
User

UserDao:

package com.zhengbin.service.test;

import java.util.List;

/**
 * Created by ZhengBin on 2017/6/30.
 */
public interface UserDao {
    User getUser(int id);
    
    List<User> getUsers(int id);
}
View Code

UserDaoImpl:

package com.zhengbin.service.test;

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

/**
 * Created by ZhengBin on 2017/6/30.
 */
public class UserDaoImpl implements UserDao {
    public User getUser(int id) {
        User user = new User();
        user.setId(id);
        user.setName("zhengbin");
        return user;
    }

    public List<User> getUsers(int id) {
        List<User> userList = new ArrayList<User>();
        User user1 = new User();
        user1.setId(1);
        user1.setName("zhengbin");
        userList.add(user1);
        User user2 = new User();
        user2.setId(2);
        user2.setName("zb");
        userList.add(user2);
        return userList;
    }
}
UserDao

UserService:

package com.zhengbin.service.test;

/**
 * Created by ZhengBin on 2017/6/30.
 */
public interface UserService {
    User getUser(int id);
}
View Code

UserServiceImpl:

package com.zhengbin.service.test;

/**
 * Created by ZhengBin on 2017/6/30.
 */
public class UserServiceImpl implements UserService {

    private UserDao userDao;

    public User getUser(int id) {
        System.out.println("doing something");
        return userDao.getUser(id);
    }
}
View Code

MockTest:

package com.zhengbin.service.test;

import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

/**
 * Created by ZhengBin on 2017/6/30.
 */
public class MockTest {
    @InjectMocks
    private UserServiceImpl userService;

    @Mock
    private UserDao userDao;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void testGetUser() {
        int id = 1;
        User user = new User();
        user.setName("mock - zb");
        user.setId(0);
        Mockito.when(userDao.getUser(1)).thenReturn(user);
        System.out.println(userService.getUser(id));
    }
}

 

posted @ 2017-06-30 23:47  郑斌blog  阅读(340)  评论(0编辑  收藏  举报