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

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

问题描述

我有一个我从请求对象填充的DTO,请求对象有很多字段。我想写一个测试来检查 populateDTO()方法是否将值放在正确的地方。如果我按照每个测试的一个断言规则,我必须编写大量测试来测试每个字段。另一种方法是在单个测试中写入多个断言。真的建议按照每个测试规则执行一个断言,或者我们可以在这些情况下放松。如何处理这个问题?

解决方案

保持分开。单位测试应该告诉你哪个单位失败。保持它们分开也允许您快速隔离问题,无需您经历漫长的调试周期。请检查以下内容...



JUnit Best实践,第2部分 - 保持分离



希望有所帮助。


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屋!

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