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();
    }
}

 

posted @ 2020-11-23 16:23  氯雷他定  阅读(282)  评论(0编辑  收藏  举报