首页 > 其他 > 详细

单元测试Service使用mockito

时间:2015-05-27 16:02:02      阅读:918      评论:0      收藏:0      [点我收藏+]

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

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


  1. maven中添加

<!-- mockito -->
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.9.5</version>
            <scope>test</scope>
        </dependency>
        <!-- junit 4 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>

2.要调用的service用@InjectMocks注解,service里面用@Mock注解

    @InjectMocks
    @Autowired
    private TestService testService;

    @Mock
    private TestMapper testMapper;

3.用@Before初始化mock
     @Before
    public void init() throws Exception {
        MockitoAnnotations.initMocks(this);
    }

4.模拟数据


        when(testMapper.getTestList()).thenReturn(list1);

        List<TestInfo> list = testService.getTestList();
        assertEquals("bname", list.get(0).getTest_name());


单元测试Service使用mockito

原文:http://2302863.blog.51cto.com/2292863/1655612

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!