SlideShare a Scribd company logo
Unit Testing in Android
By Shamsul Arafin Mahtab
Software Engineer,
M2SYS Technology
Presentation Slide
What Should we know at first
2
➢ A type of software testing where individual units/ components
of a software are tested
➢ Done during the development (coding) of an application
➢ The objective of Unit Testing is to verify its correctness
➢ Usually performed by the developer
➢ Tools that we use in Android: JUnit4
What is Unit Testing’s Job
3
Ensuring that ‘the thing’ does what it is supposed to do
Code Structure in Android
4
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class NumberValidatorTest {
/** SystemUnderTest */
NumberValidator SUT;
@Before
public void setUp() throws Exception {
SUT = new NumberValidator();
}
/** Naming unitOfWork_stateUnderTest_expectedBehaviour */
@Test
public void checkPositive_correct_trueReturned() {
}
@After
public void tearDown() throws Exception {
}
}
Press Ctrl+Shift+T for a new Test class.
Example 1
5
Example 1 – Number Validator
6
public class NumberValidatorTest {
NumberValidator SUT;
@Before
public void setUp() throws Exception {
SUT = new NumberValidator();
}
@Test
public void checkPositive_positive_trueReturned() {
boolean result = SUT.isPositiveNumber(2);
Assert.assertThat(result, CoreMatchers.is(true));
}
@Test
public void checkPositive_zero_falseReturned() {
boolean result = SUT.isPositiveNumber(0);
Assert.assertThat(result, CoreMatchers.is(false));
}
@Test
public void checkPositive_negative_falseReturned() {
boolean result = SUT.isPositiveNumber(-1);
Assert.assertThat(result, CoreMatchers.is(false));
}
}
Example 1 – Test Output
7
Current Implementation
Example 2 – Boundary Check
8
Example 2 – Overlapping Detector
9
Test Cases
1. Interval1 before Interval2 (False)
2. Interval1 overlaps Interval2 on start (True)
3. Interval1 contained on Interval2 (True)
4. Interval1 contains Interval2 (True)
5. Interval1 overlaps Interval2 on end (True)
6. Interval1 after Interval2 (False)
7. Interval1 adjacent to Interval2 on start (True)
8. Interval1 adjacent to Interval2 on end (True)
Example 2 – Overlapping Detector
10
Run IntervalsOverlapDetectorTest with Coverage
Coverage Result
Introduction To Test Doubles
11
Why Test Double man!!
12
But if you have dependencies…..
Test Double Summary
13
➢ Used to substitute external unit dependencies in tests
➢ Three types of Test Doubles: Fake, Stub, Mock
➢ Fake: Functional implementation optimized for test
➢ Stub: Returns predefined data and can have programmable
behavior
➢ Mock: Records interactions with itself
➢ Mock is most used
List of Test Frameworks
14
Language Test Frameworks Mocking Frameworks
Java JUnit Mockito
Kotlin
Spek,
KotlinTest
Mockito-Kotlin,
MockK
Some Constraints
15
➢ Unit Test is not aware of Nullability
➢ Don’t use Singleton
➢ Static Methods has hidden dependencies, so can not be
substituted
➢ Can’t test on Android Frameworks
Where can we unit test in our app
16
UI Layer
Application Layer
Domain Layer
Infrastructure Layer
Espresso Test
Black Box v White Box testing
17
Black Box Testing
White Box Testing
Which one is better?
18
Test Driven Development to Rescue?
19
Uncle Bob’s Technique
20
1. You are not allowed to write any production code unless it is
to make a failing unit test pass
2. You are not allowed to write any more of a unit test than is
sufficient to fail and compilation failures are failures
3. You are not allowed to write any more production code than it
is sufficient to pass the one failing unit test
Your Decision
21
Be honest, do you like it to do when you will implement?
My Decision after the study
22
Thank You
23

More Related Content

PPTX
Unit Testing Android Applications
KEY
Introduction to android testing
PPTX
Unit Testing And Mocking
PDF
Unit Testing
PPT
Basic Guide to Manual Testing
PPT
iOS Application Testing
PDF
Why I ❤️ Kotlin Multiplatform (and want YOU to also ❤️ Kotlin Multiplatform)
PDF
Try Jetpack Compose
Unit Testing Android Applications
Introduction to android testing
Unit Testing And Mocking
Unit Testing
Basic Guide to Manual Testing
iOS Application Testing
Why I ❤️ Kotlin Multiplatform (and want YOU to also ❤️ Kotlin Multiplatform)
Try Jetpack Compose

What's hot (20)

PDF
Events and Listeners in Android
PPTX
Unit testing
PPTX
Angular Unit Testing
PDF
Kotlin for Android Development
PDF
Test Cases for Practice.pdf
PPTX
Android jetpack compose | Declarative UI
PDF
Workshop unit test
PPTX
Mobile application testing tutorial
PPTX
Mobile application testing
PDF
Espresso testing
PPT
Google test training
PPT
Android | Android Activity Launch Modes and Tasks | Gonçalo Silva
PDF
Unit Testing with Jest
ODP
Beginners - Get Started With Unit Testing in .NET
PPTX
Automation testing
PDF
Unit Testing in Angular
PPTX
Unit testing JavaScript: Jasmine & karma intro
PPTX
Unit testing in JavaScript with Jasmine and Karma
PDF
Unit Testing in Kotlin
PPT
05 junit
Events and Listeners in Android
Unit testing
Angular Unit Testing
Kotlin for Android Development
Test Cases for Practice.pdf
Android jetpack compose | Declarative UI
Workshop unit test
Mobile application testing tutorial
Mobile application testing
Espresso testing
Google test training
Android | Android Activity Launch Modes and Tasks | Gonçalo Silva
Unit Testing with Jest
Beginners - Get Started With Unit Testing in .NET
Automation testing
Unit Testing in Angular
Unit testing JavaScript: Jasmine & karma intro
Unit testing in JavaScript with Jasmine and Karma
Unit Testing in Kotlin
05 junit
Ad

Similar to Unit Testing in Android (20)

PDF
Android testing part i
PDF
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
PDF
Unit testing, principles
PDF
Unit Testing on Android - Droidcon Berlin 2015
PPTX
2016 10-04: tdd++: tdd made easier
PPTX
Java Unit Testing
PPTX
Unit Testing in Java
PDF
Unit testing and Android
PDF
The Art of Unit Testing Feedback
PPTX
The Test way
PDF
Unit testing - A&BP CC
PPTX
L2624 labriola
PPTX
1.1 Chapter_22_ Unit Testing-testing (1).pptx
PPTX
The real beginner's guide to android testing
PDF
Mobile Development - Unit and Automation Testing
PDF
Android Building, Testing and reversing
PPT
Testing Software Engineering systems end to end
PDF
Inside Android Testing
PDF
Agile Android
PDF
Unit testing and scaffolding
Android testing part i
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
Unit testing, principles
Unit Testing on Android - Droidcon Berlin 2015
2016 10-04: tdd++: tdd made easier
Java Unit Testing
Unit Testing in Java
Unit testing and Android
The Art of Unit Testing Feedback
The Test way
Unit testing - A&BP CC
L2624 labriola
1.1 Chapter_22_ Unit Testing-testing (1).pptx
The real beginner's guide to android testing
Mobile Development - Unit and Automation Testing
Android Building, Testing and reversing
Testing Software Engineering systems end to end
Inside Android Testing
Agile Android
Unit testing and scaffolding
Ad

Recently uploaded (20)

PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
sap open course for s4hana steps from ECC to s4
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPT
Teaching material agriculture food technology
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Empathic Computing: Creating Shared Understanding
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Encapsulation theory and applications.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Machine learning based COVID-19 study performance prediction
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Encapsulation_ Review paper, used for researhc scholars
Network Security Unit 5.pdf for BCA BBA.
sap open course for s4hana steps from ECC to s4
MIND Revenue Release Quarter 2 2025 Press Release
Building Integrated photovoltaic BIPV_UPV.pdf
MYSQL Presentation for SQL database connectivity
“AI and Expert System Decision Support & Business Intelligence Systems”
Teaching material agriculture food technology
Reach Out and Touch Someone: Haptics and Empathic Computing
Empathic Computing: Creating Shared Understanding
The Rise and Fall of 3GPP – Time for a Sabbatical?
Review of recent advances in non-invasive hemoglobin estimation
Digital-Transformation-Roadmap-for-Companies.pptx
Spectral efficient network and resource selection model in 5G networks
Programs and apps: productivity, graphics, security and other tools
Encapsulation theory and applications.pdf
Chapter 3 Spatial Domain Image Processing.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Machine learning based COVID-19 study performance prediction
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows

Unit Testing in Android

  • 1. Unit Testing in Android By Shamsul Arafin Mahtab Software Engineer, M2SYS Technology Presentation Slide
  • 2. What Should we know at first 2 ➢ A type of software testing where individual units/ components of a software are tested ➢ Done during the development (coding) of an application ➢ The objective of Unit Testing is to verify its correctness ➢ Usually performed by the developer ➢ Tools that we use in Android: JUnit4
  • 3. What is Unit Testing’s Job 3 Ensuring that ‘the thing’ does what it is supposed to do
  • 4. Code Structure in Android 4 import org.junit.After; import org.junit.Before; import org.junit.Test; public class NumberValidatorTest { /** SystemUnderTest */ NumberValidator SUT; @Before public void setUp() throws Exception { SUT = new NumberValidator(); } /** Naming unitOfWork_stateUnderTest_expectedBehaviour */ @Test public void checkPositive_correct_trueReturned() { } @After public void tearDown() throws Exception { } } Press Ctrl+Shift+T for a new Test class.
  • 6. Example 1 – Number Validator 6 public class NumberValidatorTest { NumberValidator SUT; @Before public void setUp() throws Exception { SUT = new NumberValidator(); } @Test public void checkPositive_positive_trueReturned() { boolean result = SUT.isPositiveNumber(2); Assert.assertThat(result, CoreMatchers.is(true)); } @Test public void checkPositive_zero_falseReturned() { boolean result = SUT.isPositiveNumber(0); Assert.assertThat(result, CoreMatchers.is(false)); } @Test public void checkPositive_negative_falseReturned() { boolean result = SUT.isPositiveNumber(-1); Assert.assertThat(result, CoreMatchers.is(false)); } }
  • 7. Example 1 – Test Output 7 Current Implementation
  • 8. Example 2 – Boundary Check 8
  • 9. Example 2 – Overlapping Detector 9 Test Cases 1. Interval1 before Interval2 (False) 2. Interval1 overlaps Interval2 on start (True) 3. Interval1 contained on Interval2 (True) 4. Interval1 contains Interval2 (True) 5. Interval1 overlaps Interval2 on end (True) 6. Interval1 after Interval2 (False) 7. Interval1 adjacent to Interval2 on start (True) 8. Interval1 adjacent to Interval2 on end (True)
  • 10. Example 2 – Overlapping Detector 10 Run IntervalsOverlapDetectorTest with Coverage Coverage Result
  • 11. Introduction To Test Doubles 11
  • 12. Why Test Double man!! 12 But if you have dependencies…..
  • 13. Test Double Summary 13 ➢ Used to substitute external unit dependencies in tests ➢ Three types of Test Doubles: Fake, Stub, Mock ➢ Fake: Functional implementation optimized for test ➢ Stub: Returns predefined data and can have programmable behavior ➢ Mock: Records interactions with itself ➢ Mock is most used
  • 14. List of Test Frameworks 14 Language Test Frameworks Mocking Frameworks Java JUnit Mockito Kotlin Spek, KotlinTest Mockito-Kotlin, MockK
  • 15. Some Constraints 15 ➢ Unit Test is not aware of Nullability ➢ Don’t use Singleton ➢ Static Methods has hidden dependencies, so can not be substituted ➢ Can’t test on Android Frameworks
  • 16. Where can we unit test in our app 16 UI Layer Application Layer Domain Layer Infrastructure Layer Espresso Test
  • 17. Black Box v White Box testing 17 Black Box Testing White Box Testing
  • 18. Which one is better? 18
  • 19. Test Driven Development to Rescue? 19
  • 20. Uncle Bob’s Technique 20 1. You are not allowed to write any production code unless it is to make a failing unit test pass 2. You are not allowed to write any more of a unit test than is sufficient to fail and compilation failures are failures 3. You are not allowed to write any more production code than it is sufficient to pass the one failing unit test
  • 21. Your Decision 21 Be honest, do you like it to do when you will implement?
  • 22. My Decision after the study 22

Editor's Notes

  • #3: Here we can see a Popular News Paper of Bangladesh. To properly target NLP in Bengali we think to target on a specific domain such as Bangladesh topic, International topics, Politics, Economics, Sports, Entertainment, Education etc. That’s why we have target on Cricket to cover up the sports domain in Bengali Natural Language Processing.
  • #4: Here we can see a Popular News Paper of Bangladesh. To properly target NLP in Bengali we think to target on a specific domain such as Bangladesh topic, International topics, Politics, Economics, Sports, Entertainment, Education etc. That’s why we have target on Cricket to cover up the sports domain in Bengali Natural Language Processing.
  • #23: CNN is a class of deep, feed-forward artificial neural networks ( where connections between nodes do not form a cycle) & use a variation of multilayer perceptrons designed to require minimal preprocessing. These are inspired by animal visual cortex.