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
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
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

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Best Practices for Writing Unit Tests: A Comprehensive Guide
  • Exploring Unit Testing in Golang
  • Testing in DevOps – The Basic and Critical Things You Need to Know
  • Improving Customer-Facing App Quality Using Tricentis Testim

Trending

  • Streamlined Infrastructure Deployment: Harnessing the Power of Terraform and Feature Toggles
  • Effective Tips for Debugging Complex Code in Java
  • Architecting a Completely Private VPC Network and Automating the Deployment
  • Build a Serverless App Fast with Zipper: Write TypeScript, Offload Everything Else
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. Test-Driven Development (TDD) Shines with Mocking

Test-Driven Development (TDD) Shines with Mocking

Ajitesh Kumar user avatar by
Ajitesh Kumar
·
Aug. 18, 14 · Tutorial
Like (2)
Save
Tweet
Share
15.46K Views

Join the DZone community and get the full member experience.

Join For Free

the article presents a perspective and some code samples on how one could some cool stuff with test-driven development (tdd) and mocking. the code samples are done in java.

lets briefly understand what is tdd and mocking?

what is tdd?

test-driven development, simply speaking, is a software development process in which developers write tests first and, then writing enough code to pass those tests. once all of the tests pass, they do code refactoring to enhance code quality. following are key advantages of adopting tdd as your development process:

  1. enforces the developers to thoroughly think through various different test cases which could be used to test the functional/business requirements. it, thus, takes care of lot of bugs that could be found during qa and post-production phases which are very difficult to fix.
  2. developers get to work very closely with both, business analyst and test engineers. this propagates team work .
  3. code refactoring can be done in easy way.

following diagram represents tdd process:

test-driven development

test-driven development (courtesy: wikipedia)

following are some good pages on the web to get yourself going with tdd:

  • 30 days of tdd
  • introduction to test driven development (tdd)
  • test-driven development
what is mocking?

mocking is a unit testing phenomenon which helps to test objects in isolation by replacing dependent objects with complex behavior with test objects with pre-defined/simulated behavior. these test objects are called as mock objects.

how does tdd shine with mocking?

in tdd, the primary objective is to pass the tests that are written first. however, in real world scenario, as one starts writing the code, one come across various cases which could be made as dependent classes. following are different options one can do:

  • without mocking : write all code in the same class and pass the tests. once the tests pass, refactor the code by extracting methods and classes. make sure the tests pass.
  • with mocking : instead of requiring to write all the code at once in the same class, just implement the flow by making use of dependent classes and make sure the tests pass. in doing this, just define the dependent classes,  and mock them in unit tests. by doing this, one would ensure that method flow pass without having the need to write the entire code. the advantage of using mocking is the interesting behaviors that one could simulate without having need for the code. this is demonstrated later in this article with code samples. following diagram demonstrates the fact that with mocking, one may not need to write the dependent classes and still complete the code flow while making sure all the related tests pass. tdd_thumb
software requirement

we will try and see how tdd shines with mocking with this requirement. the requirement is to add a new restaurant to a restaurant database system. one should be able to add a restaurant if the mandatory data are entered.

unit test written first

looking at the requirements, one could come up with some of the following tests:

  1. restaurantnotcreatedduetovalidationfailure
  2. restaurantnotcreatedduetopersistencefailure
  3. restaurantcreated

following is how the unit test (written first) looks like:

unit test: newrestauranttest.java

public class newrestauranttest {

@before
public void setup() throws exception {
}
@after
public void teardown() throws exception {
}
@test
public void restaurantcreated() {
}
@test
public void restaurantnotcreatedduetovalidationfailure() {
}
@test
public void restaurantnotcreatedduetopersistencefailure() {
}

}
class design

looking at above tests, following different classes came into picture:

  1. newrestaurant: comprising of logic to create the restaurant
  2. restaurant: domain object representing the restaurant
  3. restaurantvalidation: consisting of validation logic matching the business rules required to add a new restaurant
  4. restaurantdao: consisting of logic to persist restaurant object into database

java class: newrestaurant.java

note the createrestaurant method in which validated and persist methods are called on classes restaurantvalidation and restaurantdao respectively. these methods are, however, empty methods in their respective classes. however, with these methods, the flow for creating the restaurant is complete. look at the code below.

public restaurant createrestaurant( restaurant restaurant ) {
restaurant retrest = null;
if( resval.validated( restaurant ) ) {
if( resdao.persist( restaurant ) ) {
retrest = new restaurant( restaurant );
}
}
return retrest;
}

to test the above code, following code demonstrates the usage of mocking. note how validated and persist methods on restaurantvalidation and restaurantdao respectively are mocked in three different tests shown below.

unit test: newrestauranttest.java

@test
public void restaurantcreated() {
restaurant restaurant = new restaurant();
mockito.when( restval.validated(restaurant)).thenreturn( true );
mockito.when( restdao.persist(restaurant)).thenreturn( true );

restaurant newrest = newrestaurant.createrestaurant(restaurant);
mockito.verify( restval ).validated(restaurant);
mockito.verify( restdao ).persist(restaurant);
assertnotnull( newrest );
}

@test
public void restaurantnotcreatedduetovalidationfailure() {
restaurant restaurant = new restaurant();
mockito.when( restval.validated(restaurant)).thenreturn( false );
mockito.when( restdao.persist(restaurant)).thenreturn( true );

restaurant newrest = newrestaurant.createrestaurant(restaurant);
mockito.verify( restval ).validated(restaurant);
mockito.verify( restdao, mockito.never() ).persist(restaurant);
assertnull( newrest );
}

@test
public void restaurantnotcreatedduetopersistencefailure() {
restaurant restaurant = new restaurant();
mockito.when( restval.validated(restaurant)).thenreturn( true );
mockito.when( restdao.persist(restaurant)).thenreturn( false );

restaurant newrest = newrestaurant.createrestaurant(restaurant);
mockito.verify( restval ).validated(restaurant);
mockito.verify( restdao ).persist(restaurant);
assertnull( newrest );
}

thus, as a summary, if you would want to have greater fun with tdd, make use of mocking as it allows you to simulate some interesting behaviors without the need for writing any code.

Test-driven development unit test Software development Pass (software)

Published at DZone with permission of Ajitesh Kumar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Best Practices for Writing Unit Tests: A Comprehensive Guide
  • Exploring Unit Testing in Golang
  • Testing in DevOps – The Basic and Critical Things You Need to Know
  • Improving Customer-Facing App Quality Using Tricentis Testim

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • 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: