首页 > 解决方案 > 使用junit在android的框架中测试业务逻辑

问题描述

我想为我的 android 应用程序编写单元测试用例,但我的业务逻辑在一个片段中。从我读到的最好将业务逻辑与android组件分开来进行单元测试。

这是我需要为其编写单元测试的功能:

 private fun loadingProject(projectDetails: ProjectDetails, isOldValueNull:Boolean, isForceLoad: Boolean) {
        if (projectDetails.Project == null)
            return
        projectDetails.Project!!.isGateway = !App.instance!!.isOnlineApp

        if(Version(projectDetails.Project?.Version).isHigherThan("5")){
            val path = Environment.getExternalStorageDirectory().absolutePath+"/"+App.instance!!.getString(R.string.app_name)+"/sList.json"
            val bufferedReader = BufferedReader(FileReader(path))
            val js  = Gson().fromJson(bufferedReader, Sensor::class.java)
            App.instance!!.info = js.Sensors
        }

        Project.saveProject(projectDetails.Project!!, isOldValueNull,  isForceLoad)
        if (projectDetails.Project!!.GatewayDeviceId == null) {
            showAlert()
        }
        if (projectDetails.SerialNos != null) {
            dType.remove(BeamItemTypes.SerialNos.name, App.instance!!.currentServerMode)
            projectDetails.SerialNos?.mapIndexed { index, it ->
                it.index = index
                it.Type = BeamItemTypes.SerialNos.name
            }
        }
    }

正如你所看到的,这是一个私有函数,它在一个片段中。测试这种功能的最佳方法是什么?

标签: androidunit-testingkotlinjunit

解决方案


推荐阅读