SlideShare a Scribd company logo
Unit test
Introduction
The goal of unit test is to isolate each part of the program and show that individual parts are correct.
Test Pyramid
Unit test cover 70% of the test. No need any android device.
Layer of an App
User Interface Layer
● Consists primarily of Android Views which aren’t Unit testable.
● UI logics are not good fit for automated testing any way.
Application layer
● User input handling and flow control
● Contains Android Components Activities, Fragments & Services which are not unit testable.
Domain Layer
● Use cases are very good abstraction for domain / business logic.
● Don’t put business logics inside Activity or Fragment.
● Don’t issue static calls from this layer
Infrastructure Layer
● Often Used 3rd
party libraries.
Unit test on which Layer
Test Doubles
● A test double is an object that can stand in for a real object in a test
● Similar to how a stunt double stands in for an actor in a movie.
Fake
● Fakes are objects that have working implementations, but not same as production one.
Usually they take some shortcut and have simplified version of production code.
● Fakes can be used when you can't use a real implementation in your test. (e.g. if the real
implementation is too slow or it talks over the network).
Stub
● Stub is an object that holds predefined data and uses it to answer calls during tests. It is used
when we cannot or don’t want to involve objects that would answer with real data or have
undesirable side effects.
● A stub has no logic, and only returns what you tell it to return. Stubs can be used when you need
an object to return specific values in order to get your code under test into a certain state.
Mock
● We use mocks when we don’t want to invoke production code or when there is no easy way to
verify, that intended code was executed.
● We don’t want to send e-mails each time we run a test. Moreover, it is not easy to verify in
tests that a right email was send. Only thing we can do is to verify the outputs of the
functionality that is exercised in our test. In other worlds, verify that e-mail sending service
was called.
Collaborators
A Collaborator Object is a unit of code whose responsibility is to orchestrate the
invocation of other units.
● Creating dependencies for unit testing. It is common in unit tests to mock or stub collaborators
of the class under test so that the test is independent of the implementation of the
collaborators.
Example
Unit test Class Body
Each unit test class must contain a setup method. The object name of the class
that is under test will be named as SUT which stands for System Under Test.
Template of class body
Unit test name pattern
<unitOfWork>_<stateUnderTest>_<expectedBehaviour>
● unitOfWork = Method Name
● stateUnderTest = what is testing
● expectedBehaviour = return value of the test
Example
Reverse_emptyString_emptyStringReturned()
Method body
@Test
public void test_customerIdPassed_nameReturned() {
//arrange
//action
//assertion
}
Arrange
On this part we will arrange all type of task that will needed to verify or sub.
Action
Method that is under system test.
Assertion
All type of assertion related tasks
Example
@Test
public void test_customerIdPassed_nameReturned() {
//arrange
Customer customer = new Customer();
customer.setFirstName(FIRST_NAME);
customer.setLastName(LAST_NAME);
when(dbManagerMock.findCustomer(CUSTOMER_ID)).thenReturn(
customer);
//action
String name = SUT.findName(CUSTOMER_ID);
//assert
assertThat(name, is(equalTo(CUSTOMER_NAME)));
}
Object vs Data Structure
Object
Expose Behavior
Hide implementation detail
Should be injected into other objects
Need to be unit tested explicitly.
Data Structure
Expose Data
No Behavior
No need to be substituted with test doubles
Reference
● https://hackernoon.com/objects-vs-data-structures-e380b962c1d2
● https://www.codingblocks.net/podcast/objects-vs-data-structures/
Test Driven Development TDD
Please follow Uncle Bob’s TDD rule to properly maintain your unit test.
Unit test documentation

More Related Content

What's hot

An Introduction to Unit Test Using NUnit
An Introduction to Unit Test Using NUnitAn Introduction to Unit Test Using NUnit
An Introduction to Unit Test Using NUnit
weili_at_slideshare
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
Sergey Podolsky
 
Unit test
Unit testUnit test
Unit test
Tran Duc
 
Unit testing and junit
Unit testing and junitUnit testing and junit
Unit testing and junit
Ömer Taşkın
 
Unit testing, UI testing and Test Driven Development in Visual Studio 2012
Unit testing, UI testing and Test Driven Development in Visual Studio 2012Unit testing, UI testing and Test Driven Development in Visual Studio 2012
Unit testing, UI testing and Test Driven Development in Visual Studio 2012Jacinto Limjap
 
NUnit Features Presentation
NUnit Features PresentationNUnit Features Presentation
NUnit Features Presentation
Shir Brass
 
Unit Tests And Automated Testing
Unit Tests And Automated TestingUnit Tests And Automated Testing
Unit Tests And Automated Testing
Lee Englestone
 
Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...
Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...
Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...
Thomas Weller
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDD
Dror Helper
 
Moq presentation
Moq presentationMoq presentation
Moq presentationLynxStar
 
Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And MockingJoe Wilson
 
Beginners - Get Started With Unit Testing in .NET
Beginners - Get Started With Unit Testing in .NETBeginners - Get Started With Unit Testing in .NET
Beginners - Get Started With Unit Testing in .NET
Baskar K
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
Joe Tremblay
 
Unit testing
Unit testingUnit testing
Unit testing
princezzlove
 
Unit Testing & TDD Training for Mobile Apps
Unit Testing & TDD Training for Mobile AppsUnit Testing & TDD Training for Mobile Apps
Unit Testing & TDD Training for Mobile Apps
Marcelo Busico
 
Unit testing in xcode 8 with swift
Unit testing in xcode 8 with swiftUnit testing in xcode 8 with swift
Unit testing in xcode 8 with swift
allanh0526
 

What's hot (20)

An Introduction to Unit Test Using NUnit
An Introduction to Unit Test Using NUnitAn Introduction to Unit Test Using NUnit
An Introduction to Unit Test Using NUnit
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Unit test
Unit testUnit test
Unit test
 
Unit testing and junit
Unit testing and junitUnit testing and junit
Unit testing and junit
 
Unit testing, UI testing and Test Driven Development in Visual Studio 2012
Unit testing, UI testing and Test Driven Development in Visual Studio 2012Unit testing, UI testing and Test Driven Development in Visual Studio 2012
Unit testing, UI testing and Test Driven Development in Visual Studio 2012
 
Unit testing, principles
Unit testing, principlesUnit testing, principles
Unit testing, principles
 
NUnit Features Presentation
NUnit Features PresentationNUnit Features Presentation
NUnit Features Presentation
 
Unit Tests And Automated Testing
Unit Tests And Automated TestingUnit Tests And Automated Testing
Unit Tests And Automated Testing
 
Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...
Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...
Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDD
 
Moq presentation
Moq presentationMoq presentation
Moq presentation
 
Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And Mocking
 
Unit testing
Unit testingUnit testing
Unit testing
 
Beginners - Get Started With Unit Testing in .NET
Beginners - Get Started With Unit Testing in .NETBeginners - Get Started With Unit Testing in .NET
Beginners - Get Started With Unit Testing in .NET
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
 
Unit test
Unit testUnit test
Unit test
 
Nunit
NunitNunit
Nunit
 
Unit testing
Unit testingUnit testing
Unit testing
 
Unit Testing & TDD Training for Mobile Apps
Unit Testing & TDD Training for Mobile AppsUnit Testing & TDD Training for Mobile Apps
Unit Testing & TDD Training for Mobile Apps
 
Unit testing in xcode 8 with swift
Unit testing in xcode 8 with swiftUnit testing in xcode 8 with swift
Unit testing in xcode 8 with swift
 

Similar to Unit test documentation

Unit Testing in Angular
Unit Testing in AngularUnit Testing in Angular
Unit Testing in Angular
Knoldus Inc.
 
Software Testing
Software TestingSoftware Testing
Software Testing
AdroitLogic
 
Unit testing
Unit testingUnit testing
Unit testing
Vinod Wilson
 
Unit testing
Unit testingUnit testing
Unit testing
Murugesan Nataraj
 
Unit testing
Unit testingUnit testing
Unit testing
Panos Pnevmatikatos
 
Unit Testing in Java
Unit Testing in JavaUnit Testing in Java
Unit Testing in Java
Ahmed M. Gomaa
 
TDD Workshop UTN 2012
TDD Workshop UTN 2012TDD Workshop UTN 2012
TDD Workshop UTN 2012
Facundo Farias
 
All Software Testing in Software Engineering
All Software Testing in Software EngineeringAll Software Testing in Software Engineering
All Software Testing in Software Engineering
sankalpkumarsahoo174
 
Android testing part i
Android testing part iAndroid testing part i
Android testing part i
Kan-Han (John) Lu
 
Java Unit Test - JUnit
Java Unit Test - JUnitJava Unit Test - JUnit
Java Unit Test - JUnit
Aktuğ Urun
 
Unit testing - An introduction
Unit testing - An introductionUnit testing - An introduction
Unit testing - An introduction
Alejandro Claro Mosqueda
 
Unit testing (Exploring the other side as a tester)
Unit testing (Exploring the other side as a tester)Unit testing (Exploring the other side as a tester)
Unit testing (Exploring the other side as a tester)
Abhijeet Vaikar
 
Unit Testing in Flutter - From Workflow Essentials to Complex Scenarios
Unit Testing in Flutter - From Workflow Essentials to Complex ScenariosUnit Testing in Flutter - From Workflow Essentials to Complex Scenarios
Unit Testing in Flutter - From Workflow Essentials to Complex Scenarios
Flutter Agency
 
Test driven development
Test driven developmentTest driven development
Test driven development
namkha87
 
Frontend training
Frontend trainingFrontend training
Frontend training
Adrian Caetano
 
Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.
Mohamed Taman
 
Unit Testing in .NET Core 7.0 with XUnit.pptx
Unit Testing in .NET Core 7.0 with XUnit.pptxUnit Testing in .NET Core 7.0 with XUnit.pptx
Unit Testing in .NET Core 7.0 with XUnit.pptx
Knoldus Inc.
 
Project Onion unit test environment
Project Onion unit test environmentProject Onion unit test environment
Project Onion unit test environment
Abhinav Jha
 
An insight to test driven development and unit testing
An insight to test driven development and unit testingAn insight to test driven development and unit testing
An insight to test driven development and unit testing
Dharmendra Prasad
 

Similar to Unit test documentation (20)

Unit testing basic
Unit testing basicUnit testing basic
Unit testing basic
 
Unit Testing in Angular
Unit Testing in AngularUnit Testing in Angular
Unit Testing in Angular
 
Software Testing
Software TestingSoftware Testing
Software Testing
 
Unit testing
Unit testingUnit testing
Unit testing
 
Unit testing
Unit testingUnit testing
Unit testing
 
Unit testing
Unit testingUnit testing
Unit testing
 
Unit Testing in Java
Unit Testing in JavaUnit Testing in Java
Unit Testing in Java
 
TDD Workshop UTN 2012
TDD Workshop UTN 2012TDD Workshop UTN 2012
TDD Workshop UTN 2012
 
All Software Testing in Software Engineering
All Software Testing in Software EngineeringAll Software Testing in Software Engineering
All Software Testing in Software Engineering
 
Android testing part i
Android testing part iAndroid testing part i
Android testing part i
 
Java Unit Test - JUnit
Java Unit Test - JUnitJava Unit Test - JUnit
Java Unit Test - JUnit
 
Unit testing - An introduction
Unit testing - An introductionUnit testing - An introduction
Unit testing - An introduction
 
Unit testing (Exploring the other side as a tester)
Unit testing (Exploring the other side as a tester)Unit testing (Exploring the other side as a tester)
Unit testing (Exploring the other side as a tester)
 
Unit Testing in Flutter - From Workflow Essentials to Complex Scenarios
Unit Testing in Flutter - From Workflow Essentials to Complex ScenariosUnit Testing in Flutter - From Workflow Essentials to Complex Scenarios
Unit Testing in Flutter - From Workflow Essentials to Complex Scenarios
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Frontend training
Frontend trainingFrontend training
Frontend training
 
Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.
 
Unit Testing in .NET Core 7.0 with XUnit.pptx
Unit Testing in .NET Core 7.0 with XUnit.pptxUnit Testing in .NET Core 7.0 with XUnit.pptx
Unit Testing in .NET Core 7.0 with XUnit.pptx
 
Project Onion unit test environment
Project Onion unit test environmentProject Onion unit test environment
Project Onion unit test environment
 
An insight to test driven development and unit testing
An insight to test driven development and unit testingAn insight to test driven development and unit testing
An insight to test driven development and unit testing
 

Recently uploaded

Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
gb193092
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 

Unit test documentation

  • 1. Unit test Introduction The goal of unit test is to isolate each part of the program and show that individual parts are correct. Test Pyramid Unit test cover 70% of the test. No need any android device. Layer of an App User Interface Layer ● Consists primarily of Android Views which aren’t Unit testable. ● UI logics are not good fit for automated testing any way.
  • 2. Application layer ● User input handling and flow control ● Contains Android Components Activities, Fragments & Services which are not unit testable. Domain Layer ● Use cases are very good abstraction for domain / business logic. ● Don’t put business logics inside Activity or Fragment. ● Don’t issue static calls from this layer Infrastructure Layer ● Often Used 3rd party libraries. Unit test on which Layer
  • 3. Test Doubles ● A test double is an object that can stand in for a real object in a test ● Similar to how a stunt double stands in for an actor in a movie.
  • 4. Fake ● Fakes are objects that have working implementations, but not same as production one. Usually they take some shortcut and have simplified version of production code. ● Fakes can be used when you can't use a real implementation in your test. (e.g. if the real implementation is too slow or it talks over the network). Stub ● Stub is an object that holds predefined data and uses it to answer calls during tests. It is used when we cannot or don’t want to involve objects that would answer with real data or have undesirable side effects. ● A stub has no logic, and only returns what you tell it to return. Stubs can be used when you need an object to return specific values in order to get your code under test into a certain state.
  • 5. Mock ● We use mocks when we don’t want to invoke production code or when there is no easy way to verify, that intended code was executed. ● We don’t want to send e-mails each time we run a test. Moreover, it is not easy to verify in tests that a right email was send. Only thing we can do is to verify the outputs of the functionality that is exercised in our test. In other worlds, verify that e-mail sending service was called.
  • 6. Collaborators A Collaborator Object is a unit of code whose responsibility is to orchestrate the invocation of other units. ● Creating dependencies for unit testing. It is common in unit tests to mock or stub collaborators of the class under test so that the test is independent of the implementation of the collaborators.
  • 7. Example Unit test Class Body Each unit test class must contain a setup method. The object name of the class that is under test will be named as SUT which stands for System Under Test.
  • 8. Template of class body Unit test name pattern <unitOfWork>_<stateUnderTest>_<expectedBehaviour> ● unitOfWork = Method Name ● stateUnderTest = what is testing ● expectedBehaviour = return value of the test
  • 9. Example Reverse_emptyString_emptyStringReturned() Method body @Test public void test_customerIdPassed_nameReturned() { //arrange //action //assertion } Arrange On this part we will arrange all type of task that will needed to verify or sub. Action Method that is under system test. Assertion All type of assertion related tasks Example @Test public void test_customerIdPassed_nameReturned() { //arrange Customer customer = new Customer(); customer.setFirstName(FIRST_NAME); customer.setLastName(LAST_NAME); when(dbManagerMock.findCustomer(CUSTOMER_ID)).thenReturn( customer); //action String name = SUT.findName(CUSTOMER_ID); //assert assertThat(name, is(equalTo(CUSTOMER_NAME))); }
  • 10. Object vs Data Structure Object Expose Behavior Hide implementation detail Should be injected into other objects Need to be unit tested explicitly. Data Structure Expose Data No Behavior No need to be substituted with test doubles Reference ● https://hackernoon.com/objects-vs-data-structures-e380b962c1d2 ● https://www.codingblocks.net/podcast/objects-vs-data-structures/ Test Driven Development TDD Please follow Uncle Bob’s TDD rule to properly maintain your unit test.