简体   繁体   English

您如何对吸气剂和吸气剂进行单元测试?

[英]How do you do unit tests for getters and setters?

I am new to tdd and the mockito web framework. 我是tdd和Mockito Web框架的新手。

Basically this is the getter method in the class: 基本上,这是该类中的getter方法:

public Long getDeviceManufactureId()
{
    return deviceManufacturerId;
}

How would I write a unit test? 我将如何编写单元测试?

so far I am thinking this: 到目前为止,我在想这个:

dem is the name of the class dem是该类的名称

@Test
public void testGetDeviceManufactureIdreturnsDeviceManufactureId()
{
    assertEquals("Richard", dem.getDeviceManufactureId());
}

In general we write test cases for methods that has some logic in it. 通常,我们为其中包含某些逻辑的方法编写测试用例。 Like service methods that may have multiple dao calls. 可能有多个dao调用的类似服务方法。 Simply writing test cases for all method does not make sense and usually wastage of time in build (however small that is). 简单地为所有方法编写测试用例是没有意义的,并且通常浪费时间在构建中(尽管如此)。 So my opinion would be not to write such trivial test cases. 因此,我的观点是不要编写如此琐碎的测试用例。

If it is just about code coverage then I am sure getter/setters will be used in some other method that will have a test for it. 如果只是关于代码覆盖率的问题,那么我可以肯定,getter / setters将在其他可以测试的方法中使用。 That should cover these. 那应该涵盖这些。 But if you absolutely have to write a test case then what you have done seems fine. 但是,如果您绝对必须编写一个测试用例,那么您所做的似乎很好。 You can also assert not null if the instance variable can never be null. 如果实例变量永远不能为null,则也可以断言not null。

Testing getters and setters could be a requirement in certain though rare scenarios where the container class is not just a POJO. 在某些尽管很少见的场景中,容器类不仅仅是一个POJO,可能需要测试getter和setter方法。

If that is a case,I would recommend creating a single TestSuite,dedicated to such instance variables,with each testcase's naming convention like : 如果是这种情况,我建议创建一个专用于此类实例变量的TestSuite,每个测试用例的命名约定如下:

TestGetVariableNameClassName
TestSetVariableNameClassName

You need to organize the test data such as maintenance is not tricky. 您需要组织测试数据,例如维护并非易事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM