zoukankan      html  css  js  c++  java
  • 8. anyInt()、anyString()、eq()、anyCollection()、verify验证void方法

    package lesson8;
    
    import org.junit.After;
    import org.junit.Assert;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.mockito.Mock;
    import org.mockito.runners.MockitoJUnitRunner;
    import java.io.Serializable;
    import java.util.Collections;
    import java.util.List;
    import static org.mockito.Matchers.*;
    import static org.mockito.Mockito.*;
    
    @RunWith(MockitoJUnitRunner.class)
    public class WildcardArgumentMatcherTest {
        @Mock
        private SimpleService simpleService;
    
        @Test
        public void wildcardTest() {
            when(simpleService.method1(anyInt(), anyString(), anyCollection(), isA(Serializable.class))).thenReturn(100);
            int result = simpleService.method1(1, "zhou", Collections.emptyList(), "Mockito");
            Assert.assertEquals(100, result);
            int result2 = simpleService.method1(2, "liu", Collections.emptySet(), "Mockito2");
            Assert.assertEquals(100, result2);
        }
    
        /**
         * 不使用anyString(),而是指定一个 eq("字符串"),当调用是字符串匹配时才返回stubbing的值
         */
        @Test
        public void wildcardTestSpecial() {
            /**注意下面三行的顺序,第一行若放在第三行则会覆盖前两行的stubbing*/
            when(simpleService.method1(anyInt(), anyString(), anyCollection(), isA(Serializable.class))).thenReturn(-1);
            when(simpleService.method1(anyInt(), eq("zhou"), anyCollection(), isA(Serializable.class))).thenReturn(100);
            when(simpleService.method1(anyInt(), eq("liu"), anyCollection(), isA(Serializable.class))).thenReturn(200);
    
            int result = simpleService.method1(1, "zhou", Collections.emptyList(), "Mockito");
            Assert.assertEquals(100, result);
            int result2 = simpleService.method1(2, "liu", Collections.emptySet(), "Mockito2");
            Assert.assertEquals(200, result2);
            int result3 = simpleService.method1(2, "other", Collections.emptySet(), "Mockito2");
            Assert.assertEquals(-1, result3);
        }
    
        @Test
        public void wildcardTestVoidMethod() {
            List<Object> emptyList = Collections.emptyList();
            doNothing().when(simpleService).method2(anyInt(), anyString(), anyCollection(), isA(Serializable.class));
            simpleService.method2(1, "zhou", emptyList, "Mockito");
            /** 方式一 */
            verify(simpleService, times(1)).method2(1, "zhou", emptyList, "Mockito");
            verify(simpleService, times(0)).method2(1, "liu", emptyList, "Mockito");
            /** 方式二 */
            verify(simpleService, times(1)).method2(anyInt(), eq("zhou"), anyCollection(), isA(Serializable.class));
        }
    
        @After
        public void destroy() {
            reset(simpleService);
        }
    }
    package lesson8;
    
    import java.io.Serializable;
    import java.util.Collection;
    
    public class SimpleService {
        public int method1(int i, String s, Collection<?> c, Serializable ser) {
            throw new RuntimeException();
        }
    
        public void method2(int i, String s, Collection<?> c, Serializable ser) {
            throw new RuntimeException();
        }
    }
  • 相关阅读:
    让VS2010/VS2012添加新类时自动添加public关键字
    Unity+MVC:实现IDependencyResolver接口需要注意的地方
    ISAPI_Rewrite引起的IIS应用程序池崩溃(fatal communication error)
    梦想成现实:用xUnit.net在单元测试中实现构造函数依赖注入
    Entity Framework 实践系列 —— 搞好关系 单相思(单向一对一,onetoone)
    在ASP.NET中运行控制台程序
    Entity Framework 实践系列 —— 搞好关系 两情相悦(双向一对一)
    程序员,用NuGet管理好你的包包
    想爱容易,相处难:当ASP.NET MVC爱上IoC
    操作步骤:用ildasm/ilasm修改IL代码
  • 原文地址:https://www.cnblogs.com/yfzhou528/p/14025174.html
Copyright © 2011-2022 走看看