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

  • SEARCH
  • Home
  • 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 3997118
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:21:46+00:00 2026-05-20T07:21:46+00:00

I have setter functions that check their input data by passing it to a

  • 0

I have setter functions that check their input data by passing it to a validation function before changing private variables. If the data is invalid, the getter functions throw an exception. How can I unit the setter functions and the data validation functions without repeating all of my input test cases for each of them? Both functions validate the data in the same way.

EDIT: Sorry, I should have included code to begin with.

setterFunction(String value) {
    if (valueValid()) {
        // Update member variable
    } else {
        throw new RuntimeException("Invalid Data");
    }
}

Both valueValid() and setterFunction fail on the same values, so I don’t want to have duplicate test code.

  • 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-20T07:21:47+00:00Added an answer on May 20, 2026 at 7:21 am

    I think it would be wise to test the validator with all test cases, and then test that the setter and getter call the validator correctly using a simple case or two.

    This way if something fails – you also have a better understanding of where the bug is (the validator or setters/getters)

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

Sidebar

Related Questions

If I have a private variable that I want to have some internal validation
I'm using Java. I want to have a setter method of one class that
Is it normal to modify setter arguments? Let's imagine that we have setString method.
Why can NHibernate create a proxy for classes with properties that have private setters
Using C++ I built a Class that has many setter functions, as well as
Imagine you have a class with dozens of private member variables. Each member variable
I have a set of variables that i need for multiple other classes. I
I have a multidimensional array. I need a function that checks if a specified
I have an AS class with setter and getter functions. I need to tweak
I have an unordered_map of functions that should be called on an object when

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.