JUnit测试POJO [英] JUnit tests for POJOs

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

问题描述

我在一个项目上工作,我们必须为所有简单的bean(POJO)创建单元测试。如果所有POJO都是getter和setter,那么为POJO创建单元测试是否有意义?假设POJO在100%的时间内都能正常工作,这是一个安全的假设吗?

I work on a project where we have to create unit tests for all of our simple beans (POJOs). Is there any point to creating a unit test for POJOs if all they consist of is getters and setters? Is it a safe assumption to assume POJOs will work about 100% of the time?

重复 - 应该对@Entity Pojos进行测试吗?

另请参阅

在数据库而不是假存储库上运行测试是不好的做法?

是否有自动测试getter和setter的Java单元测试框架?

推荐答案

TDD中的规则是测试可能会破坏的所有内容吸气剂是否会中断?一般不会,所以我不打算去测试它。此外,代码I 测试肯定会调用getter,以便 进行测试。

The rule in TDD is "Test everything that could possibly break" Can a getter break? Generally not, so I don't bother to test it. Besides, the code I do test will certainly call the getter so it will be tested.

我个人的规则是,我会为任何做出决定的函数编写一个测试,或者做一个不仅仅是微不足道的计算。我不会为<$​​ c $ c> i + 1 编写测试,但我可能会为编写if(i< 0)... 肯定会为( - b + Math.sqrt(b * b - 4 * a * c))/(2 * a)

My personal rule is that I'll write a test for any function that makes a decision, or makes more than a trivial calculation. I won't write a test for i+1, but I probably will for if (i<0)... and definitely will for (-b + Math.sqrt(b*b - 4*a*c))/(2*a).

BTW,强调POJO背后有不同的理由。我们希望将大量代码写入POJO,不依赖于它们在中运行的环境。例如,测试servlet很困难,因为它们依赖于在容器内执行。因此,我们希望servlet调用不依赖于其环境的POJO,因此易于测试。

BTW, the emphasis on POJO has a different reason behind it. We want the vast quantity of our code written into POJOs that don't depend on the environment they run in. For example, it's hard to test servlets, because they depend upon executing within a container. So we want the servlets to call POJOs that don't depend on their environment and are therefore easy to test.

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

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