单元测试mockk

单元测试

基于kotlin的mockk: https://mockk.io/

Mockito: 侧重点是纯Java代码的测试:方法调用mock,指定方法行为,截取参数,截取Callback回调

PowerMock : 支持JUnit和TestNG,扩展了EasyMock和Mockito框架,增加了mock private、static、final方法的功能。

UI自动化测试框架: 

Robolectric这是一个专门测试android系统类相关的东西的,比如UI点击事件,Actvity和Service等四大组件生命
周期,Dialog,Application,Bitmap,Resouce等进行行为测试,它之所以能测试他们,其实也是Mock了这些对
象,然后可以动态代理这些对象,不过他用的不是mock,而是ShadowXX,Android系统的每个几乎都有一个相对应
的ShadowXX,我们测试的时候得到对应的ShadowXX就可以像操作傀儡一样操纵原来的对象了,就连Loop都有
ShadowLoop专门用来测试的时候操作和调度线程的。

PowerMockito + Robolectric

Espresso

这里讲一下mockk对私用属性和方法的测试

public class Student {
    private String name = null;
    private String getName() {
        return name;
    }
    private void setName(String name) {
        this.name = name;
    }
    
    public void run(String feed) {
        System.out.println("run: " + feed);
        String result = talk();
        if (!TextUtils.equals(feed, result)) {
            feed = result;
        }
        if (name != null) {
            feed += " , " + name;
        }
        Log.i("@#@", "feed = " + feed);
        System.out.println("run end: " + feed);
    }

    private String talk() {
        System.out.println("talk");
        return "talk or run";
    }
}

测试用例:

class StudentTest {
    @ParameterizedTest
    @CsvSource(
        value = [
            "true, true, talkOrRun", // equalsValue为true,后面两个参数没意义
            "false, false, talkOrRun", // everyTalk为false,后面的参数没意义
            "false, true, talkOrRun" // talkReturn 决定最后的日志结果
        ]
    )
    /**
     * equalsValue  影响 run() 的输出是否和入参一致
     * everyTalk    是否对 talk() 方法做every操作
     * talkReturn   talk() 方法做every操作时的返回值
     */
    fun test_run(equalsValue: Boolean, everyTalk: Boolean, talkReturn: String) {
        // solved io.mockk.MockKException: no answer found for: Context(#1).getApplicationContext()
        // val mContextMock = mockk(relaxed = true)
        mockkStatic(Log::class)
        every { Log.i(any(), any()) } returns 0
        mockkStatic(TextUtils::class)
        every { TextUtils.equals(any(), any()) } returns equalsValue

        val mockStudent = spyk(Student(), recordPrivateCalls = true)
        // 设置私有属性的值
        InternalPlatformDsl.dynamicSet(mockStudent, "name", "Nancy")
        // 获取私有属性的值
        Assert.assertEquals(InternalPlatformDsl.dynamicGet(mockStudent, "name"), "Nancy")
        if (everyTalk) {
            // every插桩,对方法设定返回值,talk()方法日志没打印
            every { mockStudent.invokeNoArgs("talk") } returns talkReturn
        }
        // 调用私有方法返回值
        var value = InternalPlatformDsl.dynamicCall(mockStudent, "getName", arrayOf()) { mockk() }
        Assert.assertEquals(value, "Nancy")
        println("value: $value\n")
        // 调用私有方法设置值
        InternalPlatformDsl.dynamicCall(mockStudent, "setName", arrayOf("newName")) { mockk() } // 后面参数是协程相关的参数这里用不上
        // 调用私有方法返回值
        value = InternalPlatformDsl.dynamicCall(mockStudent, "getName", arrayOf()) { mockk() }
        println("new value: $value\n")
        //  真实调用方法
        mockStudent.run("feed")
        // 验证是否执行私有方法
        verify { mockStudent.invokeNoArgs("talk") }
    }
}

对ui文件中对单测用例

public AutoBannerLayout(Context context) {
        super(context);
        onCreateView(context);
    }

    protected void initView(View contentView) {
        adjustBannerHeight(mContentView);

        mDotNumberView = contentView.findViewById(R.id.banner_dot);
        mBannerViewPager = contentView.findViewById(R.id.banner_viewpager);
        mBannerAdapter = new AutoBannerAdapter();

        mBannerViewPager.setOnItemSelectedListener(new AutoBannerViewPager.OnItemSelectedListener() {
            @Override
            public void onItemSelected(int position) {
               
            }
        });

        mBannerAdapter.setImageViewEventCallback(new AutoBannerAdapter.ImageViewEventCallback() {
            @Override
            public void onItemClick(int position) {

            }

            @Override
            public void onItemLoadFailed(int position) {

            }
        });
        mBannerViewPager.setAdapter(mBannerAdapter);
    }
public class AutoBannerAdapter extends PagerAdapter {

    private final ArrayList mViewCaches = new ArrayList<>();
    private List mBannerList = new ArrayList<>();
    private ImageViewEventCallback imageViewEventCallback;
    private static final int MULTIPLE_BANNER_SIZE = 60;

    public void setData(List banners) {
        mBannerList.clear();
        mBannerList.addAll(banners);
        notifyDataSetChanged();
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        if (mBannerList != null && mBannerList.size() > 0) {
            int realSize = getItemCount();
            ImageView imageView = null;
            stMetaBanner banner = mBannerList.get(position % realSize);
            if (mViewCaches.isEmpty()) {
                imageView = new ImageView(container.getContext());
                ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                imageView.setLayoutParams(params);
                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); 
            } else {
                // 当缓存集合有数据时,复用,然后缓存不再持有它的引用
                imageView = mViewCaches.remove(0);
            }
            if (banner == null && imageViewEventCallback != null) {
                imageViewEventCallback.onItemLoadFailed(position % realSize);
                return imageView;
            }

            loadCoverImage(position, realSize, imageView, banner);

            imageView.setTag(R.id.tag_first, banner);
            container.addView(imageView);

            handleClickEvent(position, realSize, imageView);

            return imageView;
        }

        return null;
    }

    /**
     * 处理点击事件
     */
    private void handleClickEvent(final int position, final int realSize, ImageView imageView) {
        if (imageView == null) {
            return;
        }
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (realSize == 0) {
                    return;
                }
                int realPosition = position % realSize;
                if (imageViewEventCallback != null) {
                    imageViewEventCallback.onItemClick(realPosition);
                }
            }
        });
    }
}

单测用例

  @Test
    fun onCreateView() {
        val contextMock = mockk(relaxed = true)
        mockkStatic(LayoutInflater::class)
        mockkStatic(DeviceUtils::class)

        // Can't instantiate proxy via default constructor for class android.view.LayoutInflater
        val layoutInflaterMock = mockk()
        every { LayoutInflater.from(any()) } returns layoutInflaterMock
        every { DeviceUtils.getScreenWidth() } returns 1
        val contentViewMock = spyk(View(contextMock))
        val dotNumberViewMock = mockk()
        val autoBannerViewPagerMock = mockk()
        every { contentViewMock.findViewById(R.id.banner_dot) as DotNumberView } returns dotNumberViewMock
        every { contentViewMock.findViewById(R.id.banner_viewpager) as AutoBannerViewPager } returns autoBannerViewPagerMock
        every { layoutInflaterMock.inflate(any(), any(), any()) } returns contentViewMock
        every { contentViewMock.layoutParams } returns mockk()
        every { contentViewMock.layoutParams = any() } just runs

        every { autoBannerViewPagerMock.setOnItemSelectedListener(any()) } just runs
        every { autoBannerViewPagerMock.adapter = any() } just runs

        autoBannerViewPagerMock.setOnItemSelectedListener(AutoBannerViewPager.OnItemSelectedListener {
                position -> println("onItemSelected: $position")
        })

        val autoBannerLayoutMock = spyk(AutoBannerLayout(contextMock) )
        val autoBannerAdapterMock = InternalPlatformDsl.dynamicGet(autoBannerLayoutMock, "mBannerAdapter") as AutoBannerAdapter
        Assert.assertNotNull(autoBannerAdapterMock)

        every { dotNumberViewMock.visibility = any() } returns Unit
        every { autoBannerViewPagerMock.visibility = any() } returns Unit
        every { autoBannerViewPagerMock.stopAutoPlay() } returns Unit

        val banner = stMetaBanner(0, "", "", 1, 1, "aaa");
        val list = ArrayList()
        list.add(banner)
        autoBannerLayoutMock.setData(list, 0)
        verify { autoBannerViewPagerMock.stopAutoPlay() }
    }

    @Test
    fun instantiateItem() {
        val autoBannerViewPagerMock = spyk(AutoBannerAdapter())
        val banner = stMetaBanner(0, "bbb", "单元测试", 1, 1, "aaa");
        val list = ArrayList()
        list.add(banner)
        // 设置私有属性的值
        InternalPlatformDsl.dynamicSet(autoBannerViewPagerMock, "mBannerList", list)
        every { autoBannerViewPagerMock.instantiateItem(any(), any()) } returns mockk()

        every { autoBannerViewPagerMock.invoke("loadCoverImage") withArguments listOf(any(), any(), any(), any()) } returns Unit
        every { autoBannerViewPagerMock.invoke("handleClickEvent") withArguments listOf(any(), any(), any()) } answers {  }

        val viewGroupMock = mockk()
        val result = autoBannerViewPagerMock.instantiateItem(viewGroupMock, 0)
        Assert.assertTrue(result is ImageView)
    }

    @Test
    fun handleClickEvent() {
        // objMock should be mock or a spy to call every { ... } block on it.
        // objMock should be mock or a spy to call verify { ... } block on it.
        val autoBannerViewPagerMock = spyk(AutoBannerAdapter())
        val banner = stMetaBanner(0, "bbb", "单元测试", 1, 1, "aaa");
        val list = ArrayList()
        list.add(banner)
        // 设置私有属性的值
        InternalPlatformDsl.dynamicSet(autoBannerViewPagerMock, "mBannerList", list)
        val imageViewEventCallback = object : AutoBannerAdapter.ImageViewEventCallback {
            override fun onItemLoadFailed(position: Int) {
            }
            override fun onItemClick(position: Int) {
                println("onItemClick")
            }
        }
        autoBannerViewPagerMock.setImageViewEventCallback(imageViewEventCallback)
        val imageViewMock = spyk(ImageView(mockk()))
        InternalPlatformDsl.dynamicCall(autoBannerViewPagerMock, "handleClickEvent", arrayOf(0, 1, imageViewMock)) { mockk() }
        every { imageViewMock.callOnClick() }.run { imageViewEventCallback.onItemClick(0) }
        // io.mockk.MockKException: no answer provided for ImageView(#3).callOnClick())
        every { imageViewMock.callOnClick() } returns true
        // imageViewMock是mock的对象,单独调用有返回值的方法时,需求提前声明返回值
        // 这里的测试没意义,不是执行原文件中的回调
        imageViewMock.callOnClick()
    }

 

你可能感兴趣的:(单元测试mockk)