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
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
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);}
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"
}
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