如何使用 JUnit 测试异步进程 [英] How to use JUnit to test asynchronous processes

查看:37
本文介绍了如何使用 JUnit 测试异步进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何使用 JUnit 测试触发异步进程的方法?

How do you test methods that fire asynchronous processes with JUnit?

我不知道如何让我的测试等待进程结束(它不完全是一个单元测试,它更像是一个集成测试,因为它涉及多个类,而不仅仅是一个).

I don't know how to make my test wait for the process to end (it is not exactly a unit test, it is more like an integration test as it involves several classes and not just one).

推荐答案

恕我直言,让单元测试创​​建或等待线程等是不好的做法.您希望这些测试在几秒钟内运行.这就是为什么我想提出一种两步法来测试异步流程.

IMHO it's bad practice to have unit tests create or wait on threads, etc. You'd like these tests to run in split seconds. That's why I'd like to propose a 2-step approach to testing async processes.

  1. 测试您的异步进程是否已正确提交.您可以模拟接受异步请求的对象,并确保提交的作业具有正确的属性等.
  2. 测试您的异步回调是否在做正确的事情.在这里,您可以模拟最初提交的作业并假设它已正确初始化并验证您的回调是否正确.

这篇关于如何使用 JUnit 测试异步进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆