Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Android Test Driven Development & Android Unit Testing

Android Test Driven Development & Android Unit Testing

[Udemy 87% OFF]
https://www.udemy.com/learn-unit-testing-juni-tmockito-for-android-development/?couponCode=SP_DK_100

This is the presentation for My course on Udemy and It has the following:
Test Driven Development
Behavior Driven Design
Introduction for Junit
Introduction for Mockito

Mahmoud Ramadan

October 11, 2018
Tweet

Other Decks in Technology

Transcript

  1. About Me • Software Engineer • 6 years experience in

    Android Development • Build many apps like Chatting ,Augmented Reality, Video Calling ,streaming and many more • [email protected] • Linkedin
  2. 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.
  3. 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.
  4. 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
  5. 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
  6. 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
  7. 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
  8. Unit Testing • Speed :Fast • Cost: Less expensive •

    Tools:Junit,mockito, Power Mockito,Robolectric • Responsibility: Developer
  9. Integration Testing • Speed :Medium • Cost: medium expensive •

    Tools:mock server with okhttp • Responsibility: Developer
  10. UI Testing • Speed :slow • Cost: highly expensive •

    Tools:Espresso • Responsibility: Developer
  11. 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
  12. 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
  13. Example1:Junit Basics Business Requirement Build simple calculator to make the

    following operations • Addition • Subtraction • Division • Multiplication
  14. 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);}
  15. Example4 : Suite testing Business Requirement Build Note Taking App

    with this feature • Add text note • Add image note • Add audio note
  16. 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
  17. 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" }
  18. 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()
  19. 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)
  20. 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.
  21. 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 .
  22. Mockito can not • static method and private methods •

    constructor • No mockway equals(), hashCode() • anonymous classes, final classes
  23. [Udemy 87% OFF] Discount for my course on udemy for

    limited Time. Grab it fast Udemy DISCOUNT