SlideShare ist ein Scribd-Unternehmen logo
1 von 34
Downloaden Sie, um offline zu lesen
Test Driven Development
for Android with Junit And
Mockito
Mahmoud Ramadan
About Me
● Software Engineer
● 6 years experience in Android Development
● Build many apps like
Chatting ,Augmented Reality,
Video Calling ,streaming
and many more
● eng.mahmoudramadan2012@gmail.com
● Linkedin
Test Driven
Development
(TDD)
Why we need TDD
Working with legacy
Code
“ legacy code is a code
without test”
Why developers are scared to refactor
code ?
1. Lack of Confidence ! .They are not confident about their
changes .
2. If it doesn’t work, what amount of existing code will it
hamper ?
3. They are not sure about Regression testing.
What is TDD
Test-driven development (TDD) is an approach
for software development where you write tests
first, then use those tests to drive the design and
development of your software application.
TDD CYCLE
● Red — think about what you
want to develop
● Green — think about how to
make your tests pass
● Refactor — think about how to
improve your existing
implementation
TDD Benefits
1. Automated Testing
2. Enable Iterative development
3. Live Documentation for code
4. You feel confident about your code and logic.
5. Your code is cleaner .
6. Code quality & maintainability increases .
7. TDD can lead to more modularized, flexible and extensible code
8. Regression testing is much more easier as automated test cases find bugs
BDD(Behavior Driven Design)
● Set of best practices to write great tests
● Focus on behavior(scenario) not the implementation
● Common language between Business Analyst,Developer and Tester
● BDD comes from TDD
● Convert natural language into executable tests
● Acceptance criteria in Given/when/then syntax
Example
As a user I want to be able to search for cars that meet my criteria
Given:Cars service is initialized
When :Search for a car
Then:Results contain the car
Testing
Pyramid
White Box Testing
● Unit Testing 70%
● Integration Testing 20%
● UI Testing 10%
Unit Testing
● Speed :Fast
● Cost: Less expensive
● Tools:Junit,mockito,
Power Mockito,Robolectric
● Responsibility: Developer
Integration Testing
● Speed :Medium
● Cost: medium expensive
● Tools:mock server with okhttp
● Responsibility: Developer
UI Testing
● Speed :slow
● Cost: highly expensive
● Tools:Espresso
● Responsibility: Developer
Black Box Testing
● Stress Testing :mobile has more constraints like memory,battery and more
so make sure your app is stable when loading more data into memory or
making multiple networking calls
● Manual Testing: We still need human feedback for testing our software but
its role is to catch unexpected behavior in software like the scroll of page is
not smooth and so on
● Tools :Calabash,Appium,Monkey
Introduction to
Junit features
● Fixtures
Prerequisite for setup and teardown of tests
● Test Suites
Aggregate tests together
● Test Runners
Run Junit tests
Can aggregate success and failure for summary
Example1:Junit Basics
Business Requirement
Build simple calculator to make the following operations
● Addition
● Subtraction
● Division
● Multiplication
Example2:Parametric Testing
JUnit allows you to use parameters in your test classes. This class can contain a
test method and this method is executed with the different parameters provided.
Mark the test class as a parameterized test with the
@RunWith(Parameterized.class) annotation.
@Parameterized.Parameters
public static Collection<Object[]> data (){
Object[][] data = new Object[][]{ {1,2,3}, {4,4,8}};
return Arrays.asList(data);}
Example3:Test Lifecycle
● @BeforeClass
● @AfterClass
● @Before
● @After
Example4 : Suite testing
Business Requirement
Build Note Taking App with this feature
● Add text note
● Add image note
● Add audio note
Example 5:Rules
● A JUnit Rule can be used to do some work around the execution of a test.
● you want to connect to a database during setUp and then close the
connection in tearDown. If you want to use that database in multiple files you
don’t want to copy paste that code in every file.
● Implement Test Rule
● @Rule
Introduction to
Introduction
Problem : your Object have external dependencies
Solution :Mocking
Mockito is a Java framework allowing the creation of test double objects (mock
objects) in automated unit tests
dependencies {
testCompile "org.mockito:mockito-core:2.11.0"
}
Mocking Methods
● Static method
● Annotation
● Mockito junit Runner
● Mockito Junit Rule
Mockito Stub Test
All methods in mock object return default values like
● 0 for method returns integer
● Nothing for void method
● Null for method returns Object
So we want to simulate the return values like
thenReturn(T value) , thenThrow(Throwable... throwables), thenAnswer( Answer<?> answer),
doReturn(Object toBeReturned), doThrow(Throwable... toBeThrown), doAnswer(Answer answer),
doCallRealMethod(), doNothing()
Method name Method description
After(long millis) Verify after a given time
Timeout(long millis) Verify method execution timeout
atLeast(int minNumberOfInvocations) At least n verifications
atMost(int maxNumberOfInvocations) Perform at most n verifications
Description(String description) What to output when validation fails
Times(int wantedNumberOfInvocations) Verify the number of times the method is called
Never() Verify that the interaction did not occur, equivalent to
times(0)
Only() The verification method is only called once, which is
equivalent to times(1)
Understanding code coverage
Code coverage is a measurement of percentage of instructions of code being
executed while the automated tests are running.
A piece of code with high code coverage implies that the code has been
thoroughly unit tested and has a lower chance of containing bugs than code with a
low code coverage.
Spy
We used to mock out an object, so that when we don't
configure/save its specific behavior, the result will return an
empty type. Using the spy object (spy), then for the behavior
we don't have a stub, it will call the method of the original
object. It can be spy thought of as a partial mock .
Mockito can not
● static method and private methods
● constructor
● No mockway equals(), hashCode()
● anonymous classes, final classes
[Udemy 87% OFF]
Discount for my course on udemy for limited
Time. Grab it fast
Udemy DISCOUNT

Weitere ähnliche Inhalte

Was ist angesagt?

Elements of a Test Framework
Elements of a Test FrameworkElements of a Test Framework
Elements of a Test FrameworkSmartBear
 
Code Review Tool Evaluation
Code Review Tool EvaluationCode Review Tool Evaluation
Code Review Tool EvaluationKate Semizhon
 
Test driven development
Test driven developmentTest driven development
Test driven developmentNascenia IT
 
Test driven development_continuous_integration
Test driven development_continuous_integrationTest driven development_continuous_integration
Test driven development_continuous_integrationhaochenglee
 
New trends in testing automation
New trends in testing automationNew trends in testing automation
New trends in testing automationEran Kinsbrunner
 
Code Review
Code ReviewCode Review
Code ReviewRavi Raj
 
Static Analysis of Your OSS Project with Coverity
Static Analysis of Your OSS Project with CoverityStatic Analysis of Your OSS Project with Coverity
Static Analysis of Your OSS Project with CoveritySamsung Open Source Group
 
Binary Studio Academy: .NET Code Testing
Binary Studio Academy: .NET Code TestingBinary Studio Academy: .NET Code Testing
Binary Studio Academy: .NET Code TestingBinary Studio
 
Continuous Test Automation via CI (CodeMash 2012) - Automating the Agile way
Continuous Test Automation via CI (CodeMash 2012) - Automating the Agile wayContinuous Test Automation via CI (CodeMash 2012) - Automating the Agile way
Continuous Test Automation via CI (CodeMash 2012) - Automating the Agile wayLeonard Fingerman
 
NIWeek 2017 - Automated Test of LabVIEW FPGA Code: CI and Jenkins 2 Pipelines
NIWeek 2017 - Automated Test of LabVIEW FPGA Code: CI and Jenkins 2 PipelinesNIWeek 2017 - Automated Test of LabVIEW FPGA Code: CI and Jenkins 2 Pipelines
NIWeek 2017 - Automated Test of LabVIEW FPGA Code: CI and Jenkins 2 PipelinesChing-Hwa Yu
 
Software testing and quality assurance
Software testing and quality assuranceSoftware testing and quality assurance
Software testing and quality assuranceBenjamin Baumann
 
Generic Test Automation Architecture
Generic Test Automation ArchitectureGeneric Test Automation Architecture
Generic Test Automation ArchitectureTestingCR
 
Functional & Performance Test Automation with CI
Functional & Performance Test Automation with CI Functional & Performance Test Automation with CI
Functional & Performance Test Automation with CI Leonard Fingerman
 
Software testing
Software testingSoftware testing
Software testingK Lingaraju
 
[India Merge World Tour] Coverity
[India Merge World Tour] Coverity[India Merge World Tour] Coverity
[India Merge World Tour] CoverityPerforce
 

Was ist angesagt? (20)

Elements of a Test Framework
Elements of a Test FrameworkElements of a Test Framework
Elements of a Test Framework
 
BDD along with Continuous Integration
BDD along with Continuous IntegrationBDD along with Continuous Integration
BDD along with Continuous Integration
 
Code Review Tool Evaluation
Code Review Tool EvaluationCode Review Tool Evaluation
Code Review Tool Evaluation
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Test driven development_continuous_integration
Test driven development_continuous_integrationTest driven development_continuous_integration
Test driven development_continuous_integration
 
New trends in testing automation
New trends in testing automationNew trends in testing automation
New trends in testing automation
 
NET Code Testing
NET Code TestingNET Code Testing
NET Code Testing
 
Code Review
Code ReviewCode Review
Code Review
 
Static Analysis of Your OSS Project with Coverity
Static Analysis of Your OSS Project with CoverityStatic Analysis of Your OSS Project with Coverity
Static Analysis of Your OSS Project with Coverity
 
Binary Studio Academy: .NET Code Testing
Binary Studio Academy: .NET Code TestingBinary Studio Academy: .NET Code Testing
Binary Studio Academy: .NET Code Testing
 
SW Testing Fundamentals
SW Testing FundamentalsSW Testing Fundamentals
SW Testing Fundamentals
 
Continuous Test Automation via CI (CodeMash 2012) - Automating the Agile way
Continuous Test Automation via CI (CodeMash 2012) - Automating the Agile wayContinuous Test Automation via CI (CodeMash 2012) - Automating the Agile way
Continuous Test Automation via CI (CodeMash 2012) - Automating the Agile way
 
NIWeek 2017 - Automated Test of LabVIEW FPGA Code: CI and Jenkins 2 Pipelines
NIWeek 2017 - Automated Test of LabVIEW FPGA Code: CI and Jenkins 2 PipelinesNIWeek 2017 - Automated Test of LabVIEW FPGA Code: CI and Jenkins 2 Pipelines
NIWeek 2017 - Automated Test of LabVIEW FPGA Code: CI and Jenkins 2 Pipelines
 
Agile test practices
Agile test practicesAgile test practices
Agile test practices
 
Software testing and quality assurance
Software testing and quality assuranceSoftware testing and quality assurance
Software testing and quality assurance
 
Generic Test Automation Architecture
Generic Test Automation ArchitectureGeneric Test Automation Architecture
Generic Test Automation Architecture
 
Functional & Performance Test Automation with CI
Functional & Performance Test Automation with CI Functional & Performance Test Automation with CI
Functional & Performance Test Automation with CI
 
Software testing
Software testingSoftware testing
Software testing
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
[India Merge World Tour] Coverity
[India Merge World Tour] Coverity[India Merge World Tour] Coverity
[India Merge World Tour] Coverity
 

Ähnlich wie Android Test Driven Development & Android Unit Testing

Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...DicodingEvent
 
What is Unit Testing
What is Unit TestingWhat is Unit Testing
What is Unit TestingSadaaki Emura
 
Writing Tests with the Unity Test Framework
Writing Tests with the Unity Test FrameworkWriting Tests with the Unity Test Framework
Writing Tests with the Unity Test FrameworkPeter Kofler
 
Indy meetup#7 effective unit-testing-mule
Indy meetup#7 effective unit-testing-muleIndy meetup#7 effective unit-testing-mule
Indy meetup#7 effective unit-testing-muleikram_ahamed
 
Software Testing Basic Concepts
Software Testing Basic ConceptsSoftware Testing Basic Concepts
Software Testing Basic Conceptswesovi
 
Test driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + EclipseTest driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + EclipseUTC Fire & Security
 
Test driven development
Test driven developmentTest driven development
Test driven developmentnamkha87
 
Test-Driven Development In Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In ActionJon Kruger
 
PHPUnit with Magento
PHPUnit with MagentoPHPUnit with Magento
PHPUnit with MagentoTu Hoang
 
Testistanbul 2016 - Keynote: "Why Automated Verification Matters" by Kristian...
Testistanbul 2016 - Keynote: "Why Automated Verification Matters" by Kristian...Testistanbul 2016 - Keynote: "Why Automated Verification Matters" by Kristian...
Testistanbul 2016 - Keynote: "Why Automated Verification Matters" by Kristian...Turkish Testing Board
 
Keeping code clean
Keeping code cleanKeeping code clean
Keeping code cleanBrett Child
 
Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Gianluca Padovani
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Developmentbhochhi
 
Test-Driven Development.pptx
Test-Driven Development.pptxTest-Driven Development.pptx
Test-Driven Development.pptxTomas561914
 
Unit Testing and TDD 2017
Unit Testing and TDD 2017Unit Testing and TDD 2017
Unit Testing and TDD 2017Xavi Hidalgo
 
Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010guest5639fa9
 
Test Driven Development (TDD) with FlexUnit 4 - 360|Flex San Jose preso
Test Driven Development (TDD) with FlexUnit 4 - 360|Flex San Jose presoTest Driven Development (TDD) with FlexUnit 4 - 360|Flex San Jose preso
Test Driven Development (TDD) with FlexUnit 4 - 360|Flex San Jose presoElad Elrom
 
Test Driven Development Introduction
Test Driven Development IntroductionTest Driven Development Introduction
Test Driven Development IntroductionNguyen Hai
 

Ähnlich wie Android Test Driven Development & Android Unit Testing (20)

Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
 
What is Unit Testing
What is Unit TestingWhat is Unit Testing
What is Unit Testing
 
Writing Tests with the Unity Test Framework
Writing Tests with the Unity Test FrameworkWriting Tests with the Unity Test Framework
Writing Tests with the Unity Test Framework
 
Indy meetup#7 effective unit-testing-mule
Indy meetup#7 effective unit-testing-muleIndy meetup#7 effective unit-testing-mule
Indy meetup#7 effective unit-testing-mule
 
Software Testing Basic Concepts
Software Testing Basic ConceptsSoftware Testing Basic Concepts
Software Testing Basic Concepts
 
Test driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + EclipseTest driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + Eclipse
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
TDD Workshop UTN 2012
TDD Workshop UTN 2012TDD Workshop UTN 2012
TDD Workshop UTN 2012
 
Test-Driven Development In Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In Action
 
Python and test
Python and testPython and test
Python and test
 
PHPUnit with Magento
PHPUnit with MagentoPHPUnit with Magento
PHPUnit with Magento
 
Testistanbul 2016 - Keynote: "Why Automated Verification Matters" by Kristian...
Testistanbul 2016 - Keynote: "Why Automated Verification Matters" by Kristian...Testistanbul 2016 - Keynote: "Why Automated Verification Matters" by Kristian...
Testistanbul 2016 - Keynote: "Why Automated Verification Matters" by Kristian...
 
Keeping code clean
Keeping code cleanKeeping code clean
Keeping code clean
 
Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Tdd is not about testing (OOP)
Tdd is not about testing (OOP)
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Test-Driven Development.pptx
Test-Driven Development.pptxTest-Driven Development.pptx
Test-Driven Development.pptx
 
Unit Testing and TDD 2017
Unit Testing and TDD 2017Unit Testing and TDD 2017
Unit Testing and TDD 2017
 
Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010
 
Test Driven Development (TDD) with FlexUnit 4 - 360|Flex San Jose preso
Test Driven Development (TDD) with FlexUnit 4 - 360|Flex San Jose presoTest Driven Development (TDD) with FlexUnit 4 - 360|Flex San Jose preso
Test Driven Development (TDD) with FlexUnit 4 - 360|Flex San Jose preso
 
Test Driven Development Introduction
Test Driven Development IntroductionTest Driven Development Introduction
Test Driven Development Introduction
 

Kürzlich hochgeladen

Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 

Kürzlich hochgeladen (20)

Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 

Android Test Driven Development & Android Unit Testing

  • 1. Test Driven Development for Android with Junit And Mockito Mahmoud Ramadan
  • 2. About Me ● Software Engineer ● 6 years experience in Android Development ● Build many apps like Chatting ,Augmented Reality, Video Calling ,streaming and many more ● eng.mahmoudramadan2012@gmail.com ● Linkedin
  • 4. Why we need TDD Working with legacy Code “ legacy code is a code without test”
  • 5. Why developers are scared to refactor code ? 1. Lack of Confidence ! .They are not confident about their changes . 2. If it doesn’t work, what amount of existing code will it hamper ? 3. They are not sure about Regression testing.
  • 6. What is TDD Test-driven development (TDD) is an approach for software development where you write tests first, then use those tests to drive the design and development of your software application.
  • 7. TDD CYCLE ● Red — think about what you want to develop ● Green — think about how to make your tests pass ● Refactor — think about how to improve your existing implementation
  • 8.
  • 9. TDD Benefits 1. Automated Testing 2. Enable Iterative development 3. Live Documentation for code 4. You feel confident about your code and logic. 5. Your code is cleaner . 6. Code quality & maintainability increases . 7. TDD can lead to more modularized, flexible and extensible code 8. Regression testing is much more easier as automated test cases find bugs
  • 10. BDD(Behavior Driven Design) ● Set of best practices to write great tests ● Focus on behavior(scenario) not the implementation ● Common language between Business Analyst,Developer and Tester ● BDD comes from TDD ● Convert natural language into executable tests ● Acceptance criteria in Given/when/then syntax
  • 11. Example As a user I want to be able to search for cars that meet my criteria Given:Cars service is initialized When :Search for a car Then:Results contain the car
  • 13.
  • 14. White Box Testing ● Unit Testing 70% ● Integration Testing 20% ● UI Testing 10%
  • 15. Unit Testing ● Speed :Fast ● Cost: Less expensive ● Tools:Junit,mockito, Power Mockito,Robolectric ● Responsibility: Developer
  • 16. Integration Testing ● Speed :Medium ● Cost: medium expensive ● Tools:mock server with okhttp ● Responsibility: Developer
  • 17. UI Testing ● Speed :slow ● Cost: highly expensive ● Tools:Espresso ● Responsibility: Developer
  • 18. Black Box Testing ● Stress Testing :mobile has more constraints like memory,battery and more so make sure your app is stable when loading more data into memory or making multiple networking calls ● Manual Testing: We still need human feedback for testing our software but its role is to catch unexpected behavior in software like the scroll of page is not smooth and so on ● Tools :Calabash,Appium,Monkey
  • 20. Junit features ● Fixtures Prerequisite for setup and teardown of tests ● Test Suites Aggregate tests together ● Test Runners Run Junit tests Can aggregate success and failure for summary
  • 21. Example1:Junit Basics Business Requirement Build simple calculator to make the following operations ● Addition ● Subtraction ● Division ● Multiplication
  • 22. Example2:Parametric Testing JUnit allows you to use parameters in your test classes. This class can contain a test method and this method is executed with the different parameters provided. Mark the test class as a parameterized test with the @RunWith(Parameterized.class) annotation. @Parameterized.Parameters public static Collection<Object[]> data (){ Object[][] data = new Object[][]{ {1,2,3}, {4,4,8}}; return Arrays.asList(data);}
  • 23. Example3:Test Lifecycle ● @BeforeClass ● @AfterClass ● @Before ● @After
  • 24. Example4 : Suite testing Business Requirement Build Note Taking App with this feature ● Add text note ● Add image note ● Add audio note
  • 25. Example 5:Rules ● A JUnit Rule can be used to do some work around the execution of a test. ● you want to connect to a database during setUp and then close the connection in tearDown. If you want to use that database in multiple files you don’t want to copy paste that code in every file. ● Implement Test Rule ● @Rule
  • 27. Introduction Problem : your Object have external dependencies Solution :Mocking Mockito is a Java framework allowing the creation of test double objects (mock objects) in automated unit tests dependencies { testCompile "org.mockito:mockito-core:2.11.0" }
  • 28. Mocking Methods ● Static method ● Annotation ● Mockito junit Runner ● Mockito Junit Rule
  • 29. Mockito Stub Test All methods in mock object return default values like ● 0 for method returns integer ● Nothing for void method ● Null for method returns Object So we want to simulate the return values like thenReturn(T value) , thenThrow(Throwable... throwables), thenAnswer( Answer<?> answer), doReturn(Object toBeReturned), doThrow(Throwable... toBeThrown), doAnswer(Answer answer), doCallRealMethod(), doNothing()
  • 30. Method name Method description After(long millis) Verify after a given time Timeout(long millis) Verify method execution timeout atLeast(int minNumberOfInvocations) At least n verifications atMost(int maxNumberOfInvocations) Perform at most n verifications Description(String description) What to output when validation fails Times(int wantedNumberOfInvocations) Verify the number of times the method is called Never() Verify that the interaction did not occur, equivalent to times(0) Only() The verification method is only called once, which is equivalent to times(1)
  • 31. Understanding code coverage Code coverage is a measurement of percentage of instructions of code being executed while the automated tests are running. A piece of code with high code coverage implies that the code has been thoroughly unit tested and has a lower chance of containing bugs than code with a low code coverage.
  • 32. Spy We used to mock out an object, so that when we don't configure/save its specific behavior, the result will return an empty type. Using the spy object (spy), then for the behavior we don't have a stub, it will call the method of the original object. It can be spy thought of as a partial mock .
  • 33. Mockito can not ● static method and private methods ● constructor ● No mockway equals(), hashCode() ● anonymous classes, final classes
  • 34. [Udemy 87% OFF] Discount for my course on udemy for limited Time. Grab it fast Udemy DISCOUNT