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 33743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T13:58:10+00:00 2026-05-10T13:58:10+00:00

Lately I had to change some code on older systems where not all of

  • 0

Lately I had to change some code on older systems where not all of the code has unit tests.
Before making the changes I want to write tests, but each class created a lot of dependencies and other anti-patterns which made testing quite hard.
Obviously, I wanted to refactor the code to make it easier to test, write the tests and then change it.
Is this the way you’d do it? Or would you spend a lot of time writing the hard-to-write tests that would be mostly removed after the refactoring will be completed?

  • 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. 2026-05-10T13:58:11+00:00Added an answer on May 10, 2026 at 1:58 pm

    First of all, here’s a great article with tips on unit testing. Secondly, I found a great way to avoid making tons of changes in old code is to just refactor it a little until you can test it. One easy way to do this is to make private members protected, and then override the protected field.

    For example, let’s say you have a class that loads some stuff from the database during the constructor. In this case, you can’t just override a protected method, but you can extract the DB logic to a protected field and then override it in the test.

    public class MyClass {     public MyClass() {         // undesirable DB logic     } } 

    becomes

    public class MyClass {     public MyClass() {         loadFromDB();     }      protected void loadFromDB() {         // undesirable DB logic     } } 

    and then your test looks something like this:

    public class MyClassTest {     public void testSomething() {         MyClass myClass = new MyClassWrapper();         // test it     }      private static class MyClassWrapper extends MyClass {         @Override         protected void loadFromDB() {             // some mock logic         }     } } 

    This is somewhat of a bad example, because you could use DBUnit in this case, but I actually did this in a similar case recently because I wanted to test some functionality totally unrelated to the data being loaded, so it was very effective. I’ve also found such exposing of members to be useful in other similar cases where I need to get rid of some dependency that has been in a class for a long time.

    I would recommend against this solution if you are writing a framework though, unless you really don’t mind exposing the members to users of your framework.

    It’s a bit of a hack, but I’ve found it quite useful.

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

Sidebar

Ask A Question

Stats

  • Questions 156k
  • Answers 156k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Looking at gcc's STL, you'll find inplace_merge in stl_algo.h. This… May 12, 2026 at 10:48 am
  • Editorial Team
    Editorial Team added an answer The JQuery Cycle Plugin works great http://malsup.com/jquery/cycle/ See the tutorials… May 12, 2026 at 10:48 am
  • Editorial Team
    Editorial Team added an answer using namespace std; That's what's going on. You don't have… May 12, 2026 at 10:48 am

Related Questions

Where I work, the design and development departments are totally separated, however we (the
A new client of mine has a small VB/Access database application written in 2002
I have a PHP site that uses a fairly common authentication scheme. The entire
I've been struggling with something at work that I'm not really in the mood

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.