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

Unit Testing in Android

  • 1.
    Unit Testing inAndroid By Shamsul Arafin Mahtab Software Engineer, M2SYS Technology Presentation Slide
  • 2.
    What Should weknow 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 UnitTesting’s Job 3 Ensuring that ‘the thing’ does what it is supposed to do
  • 4.
    Code Structure inAndroid 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.
  • 5.
  • 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.
  • 12.
    Why Test Doubleman!! 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 TestFrameworks 14 Language Test Frameworks Mocking Frameworks Java JUnit Mockito Kotlin Spek, KotlinTest Mockito-Kotlin, MockK
  • 15.
    Some Constraints 15 ➢ UnitTest 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 weunit test in our app 16 UI Layer Application Layer Domain Layer Infrastructure Layer Espresso Test
  • 17.
    Black Box vWhite Box testing 17 Black Box Testing White Box Testing
  • 18.
    Which one isbetter? 18
  • 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 afterthe study 22
  • 23.

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.