• 11. verify验证方法调用


    package lesson11;
    
    import org.junit.After;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.mockito.Mock;
    import org.mockito.runners.MockitoJUnitRunner;
    import static org.hamcrest.CoreMatchers.equalTo;
    import static org.junit.Assert.assertThat;
    import static org.mockito.Mockito.*;
    
    @RunWith(MockitoJUnitRunner.class)
    public class VerifyTest {
    
        @Mock
        private SimpleDao simpleDao;
    
        @Test
        public void testRealCallAdd() {
            SimpleService simpleService = new SimpleService(simpleDao);
            Simple simple = new Simple();
            when(simpleDao.exist(simple)).thenReturn(false);
            when(simpleDao.add(simple)).thenReturn(true);
            when(simpleDao.update(simple)).thenReturn(true);
    
            boolean result = simpleService.merge(simple);
            assertThat(true, equalTo(result));
            /**
             * 利用verify确定究竟调用的是哪个方法
             */
            verify(simpleDao, times(1)).add(simple);
            verify(simpleDao, times(0)).update(simple);
        }
    
        @Test
        public void testRealCallUpdate() {
            SimpleService simpleService = new SimpleService(simpleDao);
            Simple simple = new Simple();
            when(simpleDao.exist(simple)).thenReturn(true);
            when(simpleDao.add(simple)).thenReturn(true);
            when(simpleDao.update(simple)).thenReturn(true);
    
            boolean result = simpleService.merge(simple);
            assertThat(true, equalTo(result));
            verify(simpleDao, times(0)).add(simple);
            verify(simpleDao, times(1)).update(simple);
        }
    
        @Test
        public void testMerge() {
            SimpleService simpleService = new SimpleService(simpleDao);
            Simple simple = new Simple();
            when(simpleDao.exist(simple)).thenReturn(true, false);
            when(simpleDao.add(simple)).thenReturn(true);
            when(simpleDao.update(simple)).thenReturn(true);
    
            boolean result = simpleService.merge(simple);
            assertThat(true, equalTo(result));
            verify(simpleDao, times(0)).add(simple);
            verify(simpleDao, times(1)).update(simple);
    
            result = simpleService.merge(simple);
            assertThat(true, equalTo(result));
            verify(simpleDao, times(1)).add(simple);
            verify(simpleDao, times(1)).update(simple);
        }
    
        @After
        public void destroy() {
            reset(simpleDao);
        }
    }
    package lesson11;
    
    public class SimpleService {
        public SimpleDao simpleDao;
    
        public SimpleService(SimpleDao simpleDao) {
            this.simpleDao = simpleDao;
        }
    
        public boolean merge(Simple simple) {
            boolean exist = simpleDao.exist(simple);
            if (exist) {
                return simpleDao.update(simple);
            } else {
                return simpleDao.add(simple);
            }
        }
    }
    SimpleService
    package lesson11;
    
    public class SimpleDao {
        public boolean exist(Simple simple) {
            throw new RuntimeException();
        }
    
        public boolean update(Simple simple) {
            throw new RuntimeException();
        }
    
        public boolean add(Simple simple) {
            throw new RuntimeException();
        }
    }
    SimpleDao
    package lesson11;
    
    public class Simple {
    }
    Simple
  • 相关阅读:
    NodeJS学习笔记 进阶 (11)Nodejs 进阶:调试日志打印:debug模块
    NodeJS学习笔记 进阶 (10)Nodejs 进阶:log4js入门实例(ok))
    NodeJS学习笔记 进阶 (9)express+cookie-parser:签名机制深入剖析(ok)
    NodeJS学习笔记 进阶 (8)express+morgan实现日志记录(ok)
    NodeJS学习笔记 进阶 (7)express+session实现简易身份认证(ok)
    NodeJS学习笔记 进阶 (6)本地调试远程服务器上的Node代码(ok)
    NodeJS学习笔记 进阶 (5)将图片转成datauri嵌入到html(ok)
    51nod 1287 线段树
    51nod 1043 数位dp
    51nod 1042 数位dp
  • 原文地址:https://www.cnblogs.com/yfzhou528/p/14025306.html
Copyright © 2020-2023  润新知