Encapsulation and Testability

Poorly encapsulated software is hard to test because without clear boundaries, what we actually test becomes significantly larger than what we need to test. This makes our tests run slower and it also makes them brittle so our tests are harder to maintain.

When code is unencapsulated, it can be hard to lock down behaviors in a system that we want to test. This can get so bad that we may not even be able to use a programmatic interface and must resort instead to simulating the user interacting with the system.

Manual testing or even automating user input to drive testing is a bad idea. It’s far too high a level and it ties your tests to your user interface, making them brittle. It’s far better to provide a programmatic interface that can be used to test code.

Unit testing is the first type of testing we should think of because it’s the simplest and also the most cost-effective.

Find problems early, or better yet, set up the system so we just can’t make mistakes. Encapsulation is like that. Encapsulation is a promise that a boundary is created and that nothing will penetrate that boundary. We can define an object that has public parts and private parts. The public parts can be accessed by anything or anyone but the private parts are internal, nothing on the outside can access the private information or behavior inside.

This guarantee in software languages allows us to create software that is both reliable and secure. Of course, by convention, instance data that an object holds should be marked private so that no other object can access it directly. If outside objects do need access to that data then we will provide public getters and setters.

We may, for example, want to serialize access to a particular resource so we’re granting access to only one request at a time, or we may want to just keep track of the requestors, or keep account of them, or whatever. The object that holds the state gets to decide—and that’s the point of object-oriented programming. We want objects to encapsulate their own state and be in charge of it, that is to say to contain the behaviors that access that state, which is the next code quality that we’ll be talking about: assertiveness.

Testable code tends to be well-encapsulated. It hides implementation details and can validate that behavior is correct. Testable code is code that can be tested at the unit level. When code is built with tests in this way there’s less need for other kinds of tests. A lot of the QA testing, scenario testing, and other types of non-automated testing can go away. We’re then left with a suite of tests that have all the characteristics we need: they run fast, they give the right level of feedback, and they support refactoring—all good qualities in a test base.

Unit tests run fast because we’re only testing what we need to. If the tests were written well and written to be unique, unit tests also provide the right level of feedback.

And finally, when they’re written to test behaviors rather than implementations, unit tests support refactoring. If we test behaviors and then refactor the code so we’re changing the design but not changing the behaviors our tests shouldn’t break.

To view or add a comment, sign in

More articles by David Scott Bernstein

  • Green Tests and Red Tests

    In practice, I found that there are times that I want a bit more test coverage than I get from just doing test-first…

  • Radiators and Silos

    In the old days of corporate America, the way you got ahead was through hard work and perseverance. You strove to…

    2 Comments
  • Makers and Menders

    I’ve been getting back into some research interests of mine that require data acquisition from a variety of sensors so…

  • Core Developer Practices

    Every field of engineering has a core set of practices that they follow and software engineering is no different. But…

    1 Comment
  • Still XP After All These Years

    Are you humming in your head Paul Simon’s “Still Crazy After All These Years”? I am. And it does seem crazy.

  • The Importance of Continuous Integration

    Perhaps the most important yet easiest to implement of all the software development practices in Agile is continuous…

  • The Importance of Technical Practices (Again)

    Software development has undergone many revolutions over the last few decades. The way we build software today is…

  • Summary of Seven Strategies Series

    I finished my “Seven Strategies” series of 72 blog posts with seven strategies for implementing each of the nine…

    1 Comment
  • Refactor to Learn What Not to Do

    One of the things that I was not expecting when I started refactoring other people’s code was that I started to see…

    4 Comments
  • Refactor to Clean Up Before Moving On

    Of course, the best time to refactor code is while it’s fresh in your mind, right after having worked with it. Once I…

Others also viewed

Explore content categories