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 method: public function foo($callback) { call_user_func($callback); } How would I test
Given the following function: public SomeClass(int RequestID) { // blah } Resharper complains that
Given the following code, public T Execute<T>(Func<T> methodParam) { return methodParam (); } public
Given the following declaration: <Extension()> Public Function ToJSON(ByVal target As Object) As String Dim
Given the following situation (UML below), If Y has the method: public void PrintWs();
Given the following two ways of defining a class method in Ruby: class Foo
Given the following module, module Foo def bar :baz end end def send_to_foo(method) #
Given the following case: <?php class ParentClass { public $attrA; public $attrB; public $attrC;
Is there any specific protocol for handling exceptions in public methods? Consider this eg.
I have an URL loader with the following code: public function getUploadURL():void { var

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.