【发布时间】:2016-10-21 14:18:49
【问题描述】:
我在意图上看到了不同的 UT 示例,但找不到为我的应用集成 UT 的实用方法。
请考虑这个简单的活动,它可以读取 JSON 提要并发布数据。
public class MainActivity extends Activity {
String dataArray;
/** Called when the activity is first created. */
public void onCreate() {
//Get json data from the server
getAPIdata();
}
/** get data json from the server */
public getAPIdata(Server Response){
//Parse the json output
//Validate
//populate data array
return dataArray;
}
/** use populated data array and display */
public setListView(dataArray){
//use the data array and set a list view to display the results
}
/** post data to create a record */
public postDataToServer(Parameters){
//validate input data
//send a post request to the server
//if success, print a success message on the screen
//else print the error
}
}
这是我主要在我的 Android 应用程序中使用的结构。我没有太多的业务逻辑。
让我再总结一下。
检索 JSON 并显示结果
表单输入验证
将数据发布到 API 以创建/编辑记录
所以现在您可以看到这个应用程序主要处理 CRUD 操作。我的问题是你们是:
如何在此应用程序中使用 UT? (或者这里不需要UT,因为我没有太多的业务逻辑?)
我们将不胜感激任何实用的建议。
提前致谢!
【问题讨论】:
-
Step1:关注点分离。活动不应该管理下载 json 数据。在适当的地方将逻辑拆分为不同的类
-
实际上,正如你所说,我将它放在一个单独的类中。没有发布完整的代码,因为它会很长
标签: java android unit-testing tdd automated-tests