10

Teacher wanted us to do a comprehensive unit test. For me, this will be the first time that I use JUnit. I am confused about testing set and get methods. Do you think should I test them? If the answer is yes; is this code enough for testing?

  public void testSetandGet(){
    int a = 10;
    Class firstClass = new Class();
    firstClass.setValue(10);
    int value = firstClass.getValue();
    Assert.assertTrue("Error", value==a);
  }

In my code, I think if there is an error, we can't know that the error is deriving because of setter or getter.

4
  • 1
    Consider using Assert.assertEquals(a,value) . Commented May 28, 2013 at 17:07
  • 3
    Also, it would be better if you use firstClass.setValue(a); instead of hardcoding the 10. Commented May 28, 2013 at 17:07
  • 5
    Ask your teacher if you should test them. Since it's graded, his/her opinion is the only one that matters. Commented May 28, 2013 at 17:08
  • I agree with @Cyon and others answers, but before, I would state to avoid write accessors because they violate information hiding. Commented Apr 21, 2023 at 10:40

6 Answers 6

19

I would say don't write unit tests for setters and getters, unless your setters and getters contain program logic (something getting computed as the setter or getter is called).

On a real project you will have more than plenty program logic to unit test without having to test something trivial like setters and getters.

Sign up to request clarification or add additional context in comments.

3 Comments

Sometimes people just require test only in the terms of code coverage. Of course, you can automate the testing for the accessors, but ...
True, but but this could in fact be a bad thing. Consider this: You wrote unit tests for getters and setters for all your classes but not for your important logic. Your code coverage is high but your likelihood of bugs is high as well.
Possible, indeed. Lets say it doesn't hurt to code cover the accessors, after you completely test the logic. :)
6

Roy Osherove in his famous book 'The Art Of Unit Testing' says:

Properties (getters/setters in Java) are good examples of code that usually doesn’t contain any logic, and doesn’t require testing. But watch out: once you add any check inside the property, you’ll want to make sure that logic is being tested.

You are testing what you set, can also be "get". Change your Assert statement to:

Assert.assertTrue(a, true);

Also, you should pass a, if you are going to use it.

Comments

3

I think it is important if you have some conditions in it. For exemple if it return an exception, if your int must be positive or some other conditions. But if it is just a variable assignation, I don't think it is useful... It is the same for getters.

Comments

2

If I were you, I would first set some value to a property in the class I'm testing, and then I will make sure that the get method returns the same value.

public void testSetandGet(){
    int a = 10;
    Class firstClass = new Class();
    firstClass.setValue(10);
    int value = firstClass.getValue();
    Assert.assertEquals(value, a);
}

Comments

1

Dont unit test anything that relies on things outside of the class under test to work.

If your getters and setters are trivial then they can only fail if the underlying JVM/compiler fails - so you're actually testing the JVM/compiler, not your code.

1 Comment

They can fail if someone screws up and makes a change to them inadvertently. Sure it's a small chance and setters/getters shouldn't be the first things tested, but can help a little assuming you have all business logic tested already.
1

If you wanted to test it you could temporarily make the variable public and then:

firstClass.setMyInt(5);
Assert.assertTrue(5, firstClass.myInt);

However getters and setters can be automatically generated, they typically only contain one line in the method and if they cause your code to fail you probably have bigger problems. It's not common practise with unit testing to bother testing the getters and setters and you can avoid testing them and your unit testing will still be comprehensive.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.