如何在JUnit测试中避免多个断言? [英] How to avoid multiple asserts in a JUnit test?
问题描述
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. Please check following...
JUnit Best Practices, Part 2 -- Keep 'em Separated
Hope that helps.
这篇关于如何在JUnit测试中避免多个断言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!