如何为不具有返回值的 Android Kotlin 广播接收应用编写单元测试?

huangapple go评论73阅读模式
英文:

How to write unit tests for a Android Kotlin Broadcast Receiving App which does not have return values?

问题

我已经开发了一个Android Kotlin应用程序。现在我正在创建使用JUnit和Mockito的应用程序单元测试用例。这个应用程序没有UI,运行方式类似于系统应用程序。会向该应用程序发送广播,该应用程序将处理发送的数据,并广播响应回发送的应用程序。

我无法找到编写测试的方法,因为这个应用程序没有返回值或操作(比如打开一个活动)。

如果在这种情况下编写测试用例是可能的,请提及一下。
谢谢你。

英文:

I have developed an android kotlin application. Now I am in the process of creating unit test cases for the application using JUnit and Mockito. This application does not have a UI and runs similarly to a system application. A broadcast is sent to this application which will process the sent data and a response will be broadcasted back to the sent application.

I cannot figure out a way to write a test since this application does not have a return value or an action (like opening an activity).

If it is possible to write a test case in this scenario please mention it.
Thank You

答案1

得分: 1

有三种类型的单元测试:

  • 返回值测试(返回什么?)
  • 状态测试(状态如何改变?)
  • 交互测试(它如何与其他内容进行通信?)

如果无法测试返回值,那就只剩下另外两种类型。我在这个讲座中详细描述了它们:https://youtu.be/Jzlz3Bx-NzM?t=489。正如我在讲座中所描述的,交互测试涉及到注入存根和间谍(在讲座中我称之为“模拟”):

  • 存根返回预设的数据给被测试系统
  • 间谍记录了被测试系统对其的调用方式

通常情况下,对于移动应用程序,我们处理与某个远程服务通信的请求和响应。

  • 注入一个间谍来查看应用程序是否将正确的请求发送到远程服务。状态的变化应该导致不同的请求。
  • 注入一个存根,设置为返回各种类型的响应,就好像它是远程服务一样。这使我们能够模拟各种成功和失败的情况。
英文:

There are three types of unit tests:

  • return value test (what does it return?)
  • state test (how does the state change?)
  • interaction test (how does it communicate with something else?)

If you can't test return values, that leaves the other two types. I describe these in this talk: https://youtu.be/Jzlz3Bx-NzM?t=489. And as I describe in the talk, an interaction test is where we get into injecting stubs and spies (I say "mocks" in the talk):

  • A stub returns preset data back to the System Under Test
  • A spy records how it was called by the System Under Test

Usually for mobile apps, we are dealing with Requests and Responses communicating with some remote service.

  • Inject a spy to see that the app sends the right Request to whatever the remote service is. Changes in state should result in different Requests.
  • Inject a stub that is set up to return various types of Responses, as if it were the remote service. This lets us simulate various success cases, as well as failure cases.

huangapple
  • 本文由 发表于 2023年5月22日 03:07:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76301491.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定