DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Driving DevOps With Smart, Scalable Testing
  • Unit Testing Large Codebases: Principles, Practices, and C++ Examples
  • Practical Use of Weak Symbols
  • Best Ways to Write Clean and Quality Python Code

Trending

  • Building a Real-Time Audio Transcription System With OpenAI’s Realtime API
  • Analyzing Techniques to Provision Access via IDAM Models During Emergency and Disaster Response
  • How to Merge HTML Documents in Java
  • Can You Run a MariaDB Cluster on a $150 Kubernetes Lab? I Gave It a Shot
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. Encapsulation and Testability

Encapsulation and Testability

In this article, an Agile development guru explains how the different kinds of testing function in an Agile environment, and the importance of encapsulated code.

By 
David Bernstein user avatar
David Bernstein
DZone Core CORE ·
Nov. 06, 17 · Opinion
Likes (3)
Comment
Save
Tweet
Share
6.8K Views

Join the DZone community and get the full member experience.

Join For Free

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 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 if 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.

unit test Encapsulation (networking)

Published at DZone with permission of David Bernstein, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Driving DevOps With Smart, Scalable Testing
  • Unit Testing Large Codebases: Principles, Practices, and C++ Examples
  • Practical Use of Weak Symbols
  • Best Ways to Write Clean and Quality Python Code

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!