Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3967332
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:42:26+00:00 2026-05-20T03:42:26+00:00

Given the following methods: public function setFoo($foo) { $this->_foo = $foo; return $this; }

  • 0

Given the following methods:

public function setFoo($foo) {
    $this->_foo = $foo;
    return $this;
}

public function getFoo() {
    return $this->_foo;
}

Assuming, they may be changed to be more complex in the future:

  • How would you write unit tests for those methods?
  • Just one test method?
  • Should I skip those tests?
  • What about code coverage?
  • How about @covers annotation?
  • Maybe some universal test method to implement in the abstract test case?

(I use Netbeans 7)

This seems like a waste of time, but I wouldn’t mind if IDE would generate those test methods automatically.

To qoute from the comment of Sebastian Bergman’s blog:

(it’s like testing getters and setters — fail!). In any case, if they were to fail; wouldn’t the methods that depend on on them fail?

So, what about the code coverage?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T03:42:26+00:00Added an answer on May 20, 2026 at 3:42 am

    Good Question,

    i usually try not to test getters&setters directly since i see a greater benefit in testing only the methods that actually do something.

    Especially when not using TDD this has the added benefit of showing me setters that i don’t use in my unittests showing me that ether my tests are incomplete or that the setter is not used/needed. “If i can execute all the “real” code without using that setter why is it there.”

    When using fluent setter i sometimes write a test checking the ‘fluent’ part of the setters but usually that is covered in other tests.

    To answer your list:

    • Just one test method?

    That is my least favorite option. All or none. Testing only one is not easy for other people to understand and looks ‘random’ or needs to be documented in a way.

    Edit after comment:

    Yes, for “trivial” get/set testing I’d only use one method per property maybe depending on the case even only one method for the whole class (for value objects with many getters and setters I don’t want to write/maintain many tests)

    • How would you write unit tests for those methods?
    • Should I skip those tests?

    I wouldn’t skip them. Maybe the getters depending on how many you have (i tend to write only getters i actually need) but the task of having a class completely covered shouldn’t fail because of getters.

    • What about code coverage?
    • How about @covers annotation?

    With @covers my take is always “use it everywhere or don’t use it at all”. Mixing the two ‘styles’ of testing takes away some of the benefits of the annotation and looks ‘unfinished’ to me.

    • Maybe some universal test method to implement in the abstract test case?

    For something like value objects that could work nicely. It might break (or gets more complicated) once you pass in objects / array with type hinting but I’d presonally prefer it over writing manual tests for 500 getters and setters.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following classes and controller action method: public School { public Int32 ID
Given the following Flash method: function sendToJava(name:String, ... args) { ExternalInterface.call(sendCommand, name, args); }
Given the following case: <?php class ParentClass { public $attrA; public $attrB; public $attrC;
Duplicate : Function overloading by return type? Maybe this is a very silly question
Given the following code snippet from inside a method; NSBezierPath * tempPath = [NSBezierPath
Given following Ruby statements: (Read input and store each word in array removing spaces
Given the following XML: <current> <login_name>jd</login_name> </current> <people> <person> <first>John</first> <last>Doe</last> <login_name>jd</login_name> </preson> <person>
Given the following: List<List<Option>> optionLists; what would be a quick way to determine the
Given the following example, why do I have to explicitly use the statement b->A::DoSomething()
Given the following: declare @a table ( pkid int, value int ) declare @b

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.