BlueShark
BlueShark

Reputation: 148

How do you do unit tests for getters and setters?

I am new to tdd and the mockito web framework.

Basically this is the getter method in the class:

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

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

Upvotes: 0

Views: 11079

Answers (2)

abhati
abhati

Reputation: 307

Testing getters and setters could be a requirement in certain though rare scenarios where the container class is not just a POJO.

If that is a case,I would recommend creating a single TestSuite,dedicated to such instance variables,with each testcase's naming convention like :

TestGetVariableNameClassName
TestSetVariableNameClassName

You need to organize the test data such as maintenance is not tricky.

Upvotes: 0

Aniket Thakur
Aniket Thakur

Reputation: 69025

In general we write test cases for methods that has some logic in it. Like service methods that may have multiple dao calls. 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. 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.

Upvotes: 6

Related Questions