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();
    }
}
帮我编写下面类的单元测试 package com.st.systemsettings.manager; import static com.st.systemsettings.interfa.Constant.TIME_MANAGER_REST_SWITCH; import static com.st.systemsettings.interfa.Constant.TIME_MANAGER_REST_TIME; import static com.st.systemsettings.interfa.Constant.TIME_MANAGER_WATCH_SWITCH; import static com.st.systemsettings.interfa.Constant.TIME_MANAGER_WATCH_TIME; import android.annotation.SuppressLint; import android.content.Context; import android.util.Log; import com.st.systemsettings.interfa.TimeChangeListener; import com.st.systemsettings.utils.SettingUtils; public class TimeManager { private static final String TAG = "TimeManager"; @SuppressLint("StaticFieldLeak") private static TimeManager sManager; private Context mContext; private TimeChangeListener mTimeChangeListener; public static TimeManager getInstance() { if (sManager == null) { sManager = new TimeManager(); } return sManager; } public void init(Context context) { this.mContext = context; } /** * 获取单次观看时长开关 */ public boolean getWatchSwitch() { int result = SettingUtils.getGlobalInteger(mContext, TIME_MANAGER_WATCH_SWITCH, 0); Log.i(TAG, "getWatchSwitch: result = " + result); return result == 1; } /** * 设置单次观看时长开关 */ public void setWatchSwitch(boolean open) { Log.i(TAG, "setWatchSwitch: open = " + open); SettingUtils.putGlobalInteger(mContext, TIME_MANAGER_WATCH_SWITCH, open ? 1 : 0); if (mTimeChangeListener != null) { mTimeChangeListener.onWatchSwitchChanged(open); } } /** * 获取观看时长 */ public int getWatchTime() { int result = SettingUtils.getGlobalInteger(mContext, TIME_MANAGER_WATCH_TIME, 30); Log.i(TAG, "getWatchTime: result = " + result); return result; } /** * 设置观看时长 */ public void setWatchTime(int time) { Log.i(TAG, "setWatchTime: time = " + time); SettingUtils.putGlobalInteger(mContext, TIME_MANAGER_WATCH_TIME, time); if (mTimeChangeListener != null) { mTimeChangeListener.onWatchTimeChanged(time); } } /** * 获取单次休息时长开关 */ public boolean getRestSwitch() { int result = SettingUtils.getGlobalInteger(mContext, TIME_MANAGER_REST_SWITCH, 0); Log.i(TAG, "getRestSwitch: result = " + result); return result == 1; } /** * 设置单次休息时长开关 */ public void setRestSwitch(boolean open) { Log.i(TAG, "setRestSwitch: open = " + open); SettingUtils.putGlobalInteger(mContext, TIME_MANAGER_REST_SWITCH, open ? 1 : 0); if (mTimeChangeListener != null) { mTimeChangeListener.onRestSwitchChanged(open); } } /** * 获取休息时长 */ public int getRestTime() { int result = SettingUtils.getGlobalInteger(mContext, TIME_MANAGER_REST_TIME, 5); Log.i(TAG, "getRestTime: result = " + result); return result; } /** * 设置休息时长 */ public void setRestTime(int time) { Log.i(TAG, "setRestTime: time = " + time); SettingUtils.putGlobalInteger(mContext, TIME_MANAGER_REST_TIME, time); if (mTimeChangeListener != null) { mTimeChangeListener.onRestTimeChanged(time); } } public void setTimeChangeListener(TimeChangeListener timeChangeListener) { mTimeChangeListener = timeChangeListener; } }
最新发布
07-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值