SlideShare a Scribd company logo
Dev Unit Test and TDD
What is “Unit Testing”
 Unit test is examining the smallest testable unit of
your code.
 It is not about finding bugs, it might be but
secondary to examining and is different then
Manual/QA/Integration testing.
 Unit testing should be part of design process similar
to TDD (Test Driven Development).
Objectives
 Fail first/fast
 Code coverage
 TDD
Name it clearly
 Test class
 Test class for service class should be named as service class
name with suffix “Test”
 E.g. MemberServiceTest for MemberService Class
 Test case/method
 Test case should be named after test unit name with prefix
“test”
 Choose meaningful names
 E.g. testFindMember() for findMember(Integer memberId)
 E.g. testFindMemberNotUniqueResult()
What to test
 Positive scenarios
 Negative scenarios
 Borderline cases
One code unit at a time
 One code unit can have one to many test cases
 Write test for method that have the fewest
dependencies first, and work your way up
 Change proof, very rarely need to change exiting test
case or very minimum change required.
Use “Assertions”, avoid “Print”
statements
 No use of print statements, except development
phase.
 Makes test case cluttered, difficult to read.
Avoid unnecessary assertions
 As unit testing is part of design, write your test case
only to cover one (at max two, including otherwise
condition) assertion.
Test case must be independent
 Never try to interlink test cases
 Test case should be independent of other test case so
that change in one will not impact other.
Try to use Mock
 Wherever possible try to use Mock-API (Mockito) to
mock underlying/external resources, persistence
calls
 Avoid assumptions or having constants inputs for
dynamic, it may or may not work
 Such test cases will prove useless
Externalize test data
 Maintain external resource like properties file to
externalize test data.
 TestNG – Has feature to do it, Arch Team will decide
on it.
Avoid unit test for Configuration/Master
data
 It is obvious that configuration will not be going to
change.
 Single test case will suffice.
Ensure that test code is separated from
production code
 In your build script, ensure that test code is not
deployed with actual source code. Its a wastage of
resource.
 This is for Arch Team. We are working on with
Maven in place.
Do not skip unit tests
 Do not use @Ignore or comment (//) to skip
execution.
 If some test cases are not valid now then remove
them from your source code. Having invalid/static
test cases in source code will not help anyone.
Lets strive for TDD
Thank You
Session to follow
1. Practical Junit
2. Use of Mockito
3. TestNG – More features over Junit

More Related Content

PDF
Unit Testing Done Right
PPTX
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
PPTX
Unit Testing Concepts and Best Practices
PDF
Unit Testing Best Practices
PPTX
Test driven development in .Net - 2010 + Eclipse
PPTX
Moq presentation
PPTX
Unit Testing And Mocking
PPT
Unit Testing
Unit Testing Done Right
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
Unit Testing Concepts and Best Practices
Unit Testing Best Practices
Test driven development in .Net - 2010 + Eclipse
Moq presentation
Unit Testing And Mocking
Unit Testing

What's hot (20)

PDF
Unit Testing Fundamentals
PDF
Workshop unit test
PPTX
Unit Tests And Automated Testing
PPTX
Unit Testing (C#)
PPTX
Unit tests & TDD
PPTX
An Introduction to Unit Testing
PPTX
Unit Testing
PPTX
Roy Osherove TDD From Scratch
PPTX
Understanding Unit Testing
PPT
Unit testing
PPSX
Unit Test Presentation
PPTX
Unit testing - the hard parts
PDF
Clean Unit Test Patterns
PPT
Automated Unit Testing
PPTX
Unit test
PPT
Unit testing
PPTX
Benefit From Unit Testing In The Real World
PDF
Unit and integration Testing
PPT
New Features Of Test Unit 2.x
PPTX
Unit testing, UI testing and Test Driven Development in Visual Studio 2012
Unit Testing Fundamentals
Workshop unit test
Unit Tests And Automated Testing
Unit Testing (C#)
Unit tests & TDD
An Introduction to Unit Testing
Unit Testing
Roy Osherove TDD From Scratch
Understanding Unit Testing
Unit testing
Unit Test Presentation
Unit testing - the hard parts
Clean Unit Test Patterns
Automated Unit Testing
Unit test
Unit testing
Benefit From Unit Testing In The Real World
Unit and integration Testing
New Features Of Test Unit 2.x
Unit testing, UI testing and Test Driven Development in Visual Studio 2012
Ad

Similar to Unit Testing (20)

PDF
Unit testing, principles
PPS
Why Unit Testingl
PPS
Why Unit Testingl
PPS
Why unit testingl
PPS
Unit Testing
PPTX
UNIT TESTING PPT
PPTX
TDD Best Practices
PDF
Unit testing - An introduction
PPTX
Unit testing
PPTX
Unit testing basics with NUnit and Visual Studio
PPTX
Unit testing & TDD concepts with best practice guidelines.
PPTX
Unit Testing and TDD 2017
PPTX
Implementing TDD in for .net Core applications
PDF
Software Testing
PPT
An insight to test driven development and unit testing
ODP
Effective unit testing
PPTX
JavaScript Unit Testing
PDF
What Is Unit Testing_ A Complete Guide With Examples.pdf
PDF
What Is Unit Testing A Complete Guide With Examples.pdf
PDF
Unit testing (Exploring the other side as a tester)
Unit testing, principles
Why Unit Testingl
Why Unit Testingl
Why unit testingl
Unit Testing
UNIT TESTING PPT
TDD Best Practices
Unit testing - An introduction
Unit testing
Unit testing basics with NUnit and Visual Studio
Unit testing & TDD concepts with best practice guidelines.
Unit Testing and TDD 2017
Implementing TDD in for .net Core applications
Software Testing
An insight to test driven development and unit testing
Effective unit testing
JavaScript Unit Testing
What Is Unit Testing_ A Complete Guide With Examples.pdf
What Is Unit Testing A Complete Guide With Examples.pdf
Unit testing (Exploring the other side as a tester)
Ad

Recently uploaded (20)

PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
medical staffing services at VALiNTRY
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
AI in Product Development-omnex systems
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
ISO 45001 Occupational Health and Safety Management System
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
L1 - Introduction to python Backend.pptx
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
top salesforce developer skills in 2025.pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
System and Network Administration Chapter 2
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Design an Analysis of Algorithms II-SECS-1021-03
2025 Textile ERP Trends: SAP, Odoo & Oracle
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
medical staffing services at VALiNTRY
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
AI in Product Development-omnex systems
Navsoft: AI-Powered Business Solutions & Custom Software Development
ISO 45001 Occupational Health and Safety Management System
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
VVF-Customer-Presentation2025-Ver1.9.pptx
L1 - Introduction to python Backend.pptx
How to Migrate SBCGlobal Email to Yahoo Easily
top salesforce developer skills in 2025.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
System and Network Administration Chapter 2
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Upgrade and Innovation Strategies for SAP ERP Customers
Design an Analysis of Algorithms II-SECS-1021-03

Unit Testing

  • 1. Dev Unit Test and TDD
  • 2. What is “Unit Testing”  Unit test is examining the smallest testable unit of your code.  It is not about finding bugs, it might be but secondary to examining and is different then Manual/QA/Integration testing.  Unit testing should be part of design process similar to TDD (Test Driven Development).
  • 3. Objectives  Fail first/fast  Code coverage  TDD
  • 4. Name it clearly  Test class  Test class for service class should be named as service class name with suffix “Test”  E.g. MemberServiceTest for MemberService Class  Test case/method  Test case should be named after test unit name with prefix “test”  Choose meaningful names  E.g. testFindMember() for findMember(Integer memberId)  E.g. testFindMemberNotUniqueResult()
  • 5. What to test  Positive scenarios  Negative scenarios  Borderline cases
  • 6. One code unit at a time  One code unit can have one to many test cases  Write test for method that have the fewest dependencies first, and work your way up  Change proof, very rarely need to change exiting test case or very minimum change required.
  • 7. Use “Assertions”, avoid “Print” statements  No use of print statements, except development phase.  Makes test case cluttered, difficult to read.
  • 8. Avoid unnecessary assertions  As unit testing is part of design, write your test case only to cover one (at max two, including otherwise condition) assertion.
  • 9. Test case must be independent  Never try to interlink test cases  Test case should be independent of other test case so that change in one will not impact other.
  • 10. Try to use Mock  Wherever possible try to use Mock-API (Mockito) to mock underlying/external resources, persistence calls  Avoid assumptions or having constants inputs for dynamic, it may or may not work  Such test cases will prove useless
  • 11. Externalize test data  Maintain external resource like properties file to externalize test data.  TestNG – Has feature to do it, Arch Team will decide on it.
  • 12. Avoid unit test for Configuration/Master data  It is obvious that configuration will not be going to change.  Single test case will suffice.
  • 13. Ensure that test code is separated from production code  In your build script, ensure that test code is not deployed with actual source code. Its a wastage of resource.  This is for Arch Team. We are working on with Maven in place.
  • 14. Do not skip unit tests  Do not use @Ignore or comment (//) to skip execution.  If some test cases are not valid now then remove them from your source code. Having invalid/static test cases in source code will not help anyone.
  • 16. Thank You Session to follow 1. Practical Junit 2. Use of Mockito 3. TestNG – More features over Junit

Editor's Notes

  • #3: Writing fast, bug-free code is easy, if it’s a) not too complex and b) written once, then never touched again. Of course, if you’re working on anything other than a throw-away mobile game, this is never the case. So in order to keep your code as high quality and maintainable as possible, you need tests. Tests allow you to add new features, without breaking what’s already there. Tests help you to make changes in your architecture, without damaging behavior. Tests enable you to find newly introduced bugs early and with little additional effort.