如何在 JUnit 测试中避免多个断言? [英] How to avoid multiple asserts in a JUnit test?

查看:34
本文介绍了如何在 JUnit 测试中避免多个断言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从请求对象填充的 DTO,请求对象有很多字段.我想编写一个测试来检查 populateDTO() 方法是否将值放在正确的位置.如果我遵循每个测试一个断言的规则,我将不得不编写大量测试来测试每个字段.另一种方法是在单个测试中编写多个断言.是否真的建议每个测试规则遵循一个断言,或者我们可以在这些情况下放松.我该如何解决这个问题?

I have a DTO which I'm populating from the request object, and the request object has many fields. I want to write a test to check if the populateDTO() method is putting values in the right places or not. If I follow the rule of one assert per test, I would have to write a large number of tests, to test each field. The other approach would be to write multiple asserts in a single test. Is it really recommended to follow one assert per test rule or can we relax in these cases. How do I approach this problem?

推荐答案

将它们分开.单元测试应该告诉你哪个单元失败了.将它们分开还可以让您快速隔离问题,而无需经历漫长的调试周期.

Keep them separate. A unit test is supposed to tell you which unit failed. Keeping them separate also allows you to isolate the problem quickly w/o requiring you to go through a lengthy debug cycle.

这篇关于如何在 JUnit 测试中避免多个断言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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