Mock Based Unit Testing

Embed Size (px)

Citation preview

  • 7/27/2019 Mock Based Unit Testing

    1/17

    Unit/Scenario Testing Approaches

    Udayan Datta

    I079774

  • 7/27/2019 Mock Based Unit Testing

    2/17

    2011 SAP AG. All rights reserved. 2

    Agenda/Scopes

    Introduction of Mock Framework & Demo

    How to mock and do unit & scenario tests both

    How to design the effective test cases by using this mock framework

    Salient available features (mock, spy, stub, verify, any.. etc..) in mock framework

    How to test the complex UI parts/Complex interdependent/recursive/substantial code part with

    mocking

    Plans/Milestones for next to achieve 100% UI UNIT Test cases for the all the UI modules in SP7

    Sprint 2 onwards An implementation plan for Sprint 2 (SP7)

    Questions/Answers/Finalization

  • 7/27/2019 Mock Based Unit Testing

    3/17

    2011 SAP AG. All rights reserved. 3

    Intoduction of Mock Framework and Demo

    We are using Mockito framework along with PowerMock for additional features

    Benefits

    o Mockito makes mocking very easy.

    o It is extremely easy to read mock code.

    o Mockito works in any environment and has no external dependencies

    Drawbacks

    o Mockito has all the same limitations, which manually created mocks have. It is

    impossible to mock final classes or static methods.

    Demo

  • 7/27/2019 Mock Based Unit Testing

    4/17

    2011 SAP AG. All rights reserved. 4

    Why we need to mock for Unit and Scenario Test?

    The salient features which should be available with the mock framework

    Focused testing

    Separation of stubbing and verification

    o Dont want any verification-related code before execution.

    Explicit language

    o Verification and stubbing code should be easy to discern, should distinguish from

    ordinary method calls / from any supporting code / assertions

    Simple stubbing model

    Only one type of mock, one way of creating mocks

    No framework-supporting code

    o No record(), replay() methods, no alien control/context objects.

    Slim API

    o So that anyone can use it efficiently and produce clean code

    Explicit errorso Verification errors should always point stack trace to failed interaction

    Clean stack trace

  • 7/27/2019 Mock Based Unit Testing

    5/17

    2011 SAP AG. All rights reserved. 5

    Why we need to mock for Unit and Scenario Test?

    One of significant challenges is the problem of huge unreadable code.

    This problem can be solved easily with Mockito library. It generates mocks using

    reflection.

    Mockito records all mock invocations, including methods arguments.

    Mockito provides several ways to define mock behavior, and several ways to validate

    invocations occurred.

    Mockito works in any environment. The only thing required is the presence of Mockito

    JAR in class path.

  • 7/27/2019 Mock Based Unit Testing

    6/17

    2011 SAP AG. All rights reserved. 6

    Mockito Features

    Verifying code behaviors

    Stubbing the mock values

    Argument matchers

    Verifying exact number of invocations / at least x / never

    Stubbing void methods with exceptions

    Verification in order

    Changing default return values of unstubbed invocations Finding redundant invocations

    Shorthand for mocks creation - @Mock annotation

    Stubbing consecutive calls (iterator-style stubbing)

    Stubbing with callbacks

    doThrow()|doAnswer()|doNothing()|doReturn() family of methods for

    stubbing voids (mostly)

    Spying on real objects

    Capturing arguments for further assertions

  • 7/27/2019 Mock Based Unit Testing

    7/17 2011 SAP AG. All rights reserved. 7

    Mockito Features

    Real partial mocks

    Resetting mocks

    Troubleshooting & validating framework usage

    Aliases for behavior driven development

    (**New**) Serializable mocks

    For more advanced features - Powermock is also an advanced version of

    mocking framework which will give more testable features on top of currentMockito - http://blog.griddynamics.com/2010/03/mocking-with-mockito-and-

    powermock.html

    While Mockito does not support Static method & final class, PowerMock is supporting

    these. PowerMock can work on top of Mockito.

    Mockito Features -

    http://mockito.googlecode.com/svn/tags/latest/javadoc/org/mockito/Mockito.htm

    l

    http://blog.griddynamics.com/2010/03/mocking-with-mockito-and-powermock.htmlhttp://blog.griddynamics.com/2010/03/mocking-with-mockito-and-powermock.htmlhttp://mockito.googlecode.com/svn/tags/latest/javadoc/org/mockito/Mockito.htmlhttp://mockito.googlecode.com/svn/tags/latest/javadoc/org/mockito/Mockito.htmlhttp://mockito.googlecode.com/svn/tags/latest/javadoc/org/mockito/Mockito.htmlhttp://mockito.googlecode.com/svn/tags/latest/javadoc/org/mockito/Mockito.htmlhttp://mockito.googlecode.com/svn/tags/latest/javadoc/org/mockito/Mockito.htmlhttp://mockito.googlecode.com/svn/tags/latest/javadoc/org/mockito/Mockito.htmlhttp://blog.griddynamics.com/2010/03/mocking-with-mockito-and-powermock.htmlhttp://blog.griddynamics.com/2010/03/mocking-with-mockito-and-powermock.htmlhttp://blog.griddynamics.com/2010/03/mocking-with-mockito-and-powermock.htmlhttp://blog.griddynamics.com/2010/03/mocking-with-mockito-and-powermock.htmlhttp://blog.griddynamics.com/2010/03/mocking-with-mockito-and-powermock.htmlhttp://blog.griddynamics.com/2010/03/mocking-with-mockito-and-powermock.htmlhttp://blog.griddynamics.com/2010/03/mocking-with-mockito-and-powermock.htmlhttp://blog.griddynamics.com/2010/03/mocking-with-mockito-and-powermock.htmlhttp://blog.griddynamics.com/2010/03/mocking-with-mockito-and-powermock.htmlhttp://blog.griddynamics.com/2010/03/mocking-with-mockito-and-powermock.html
  • 7/27/2019 Mock Based Unit Testing

    8/17 2011 SAP AG. All rights reserved. 8

    Mockito Features

    Real partial mocks

    Resetting mocks

    Troubleshooting & validating framework usage

    Aliases for behavior driven development

    (**New**) Serializable mocks

    For more advanced features - Powermock is also an advanced version of

    mocking framework which will give more testable features on top of currentMockito - http://blog.griddynamics.com/2010/03/mocking-with-mockito-and-

    powermock.html

    Mocking Static Method

    Partial Mocking

    o We can use PowerMockito to partially mock a method using PowerMockito.spy

    Mockito Reference -http://mockito.googlecode.com/svn/tags/latest/javadoc/org/mockito/Mockito.html

    http://blog.griddynamics.com/2010/03/mocking-with-mockito-and-powermock.htmlhttp://blog.griddynamics.com/2010/03/mocking-with-mockito-and-powermock.htmlhttp://mockito.googlecode.com/svn/tags/latest/javadoc/org/mockito/Mockito.htmlhttp://mockito.googlecode.com/svn/tags/latest/javadoc/org/mockito/Mockito.htmlhttp://mockito.googlecode.com/svn/tags/latest/javadoc/org/mockito/Mockito.htmlhttp://mockito.googlecode.com/svn/tags/latest/javadoc/org/mockito/Mockito.htmlhttp://blog.griddynamics.com/2010/03/mocking-with-mockito-and-powermock.htmlhttp://blog.griddynamics.com/2010/03/mocking-with-mockito-and-powermock.htmlhttp://blog.griddynamics.com/2010/03/mocking-with-mockito-and-powermock.htmlhttp://blog.griddynamics.com/2010/03/mocking-with-mockito-and-powermock.htmlhttp://blog.griddynamics.com/2010/03/mocking-with-mockito-and-powermock.htmlhttp://blog.griddynamics.com/2010/03/mocking-with-mockito-and-powermock.htmlhttp://blog.griddynamics.com/2010/03/mocking-with-mockito-and-powermock.htmlhttp://blog.griddynamics.com/2010/03/mocking-with-mockito-and-powermock.htmlhttp://blog.griddynamics.com/2010/03/mocking-with-mockito-and-powermock.htmlhttp://blog.griddynamics.com/2010/03/mocking-with-mockito-and-powermock.html
  • 7/27/2019 Mock Based Unit Testing

    9/17 2011 SAP AG. All rights reserved. 9

    Design of Effective Scenario Test

    Test Workflow Identify Interactions

    Create Mocks

    oMultiple level of mocks can be created and stubbed values

    Stub the required mock objects

    Invoke the mock object Verify/Validate the object call and output

    Assert Expectations from the Validation

  • 7/27/2019 Mock Based Unit Testing

    10/17 2011 SAP AG. All rights reserved. 10

    An implementation plan for Sprint 2 (SP7) Onwards

    TBD

  • 7/27/2019 Mock Based Unit Testing

    11/17 2011 SAP AG. All rights reserved. 11

    Q/A/F

    Q/A Finalization

    Thank You

  • 7/27/2019 Mock Based Unit Testing

    12/17 2011 SAP AG. All rights reserved. 12

    Categories of modeling elements

    SQL view

    Joins

    Unions

    Filter

    Calculations

    Group by

    Order by

    SQL view ++

    Scripts

    Curr Conv.

    Parameters

    Key

    Non aggregatable

    column

    Advanced Semantic

    (multi dim)

    Dimensions

    Measures

    Variables for client

    tools

    Basic Semantic (possible also on

    table and catalog level)

    Column semantic

    Language (SAP, ISO, Locale)

    Client (MANDT)

    Amount, Quantity

    Unit / Currency

    Time / Date Semantic

    Assignment to dependent columns

    Description

    Unit / Currency

    Hierarchies (requires a key)

    Leveled

    Parent Child

    Attribute relationships

  • 7/27/2019 Mock Based Unit Testing

    13/17 2011 SAP AG. All rights reserved. 13

    UI

    Keep the paradigm of the current calc view editor and use that to model the

    new view

    Include a Data Foundation Star join node for easier join modeling

    Overview Node Detail Node columns

  • 7/27/2019 Mock Based Unit Testing

    14/17 2011 SAP AG. All rights reserved. 14

    UI

    Analytic view in the new editor with two Multi join / Star join Node

    Data

    Foundation

    Detail of JoinDFData Foundation

    Old New

    Data

    Found

    ation

    Detail of JoinDIM

  • 7/27/2019 Mock Based Unit Testing

    15/17 2011 SAP AG. All rights reserved. 15

    Capabilities

    All semantic modeling would be done in the output node? (Maybealso on the inner nodes)

    Everything that is possible in calc view or analytic view editor and

    more would be offered there including

    Add views as Shared dimensions

    Counters

    Measures and aggregation

    Currency conversion can also be done inside

    Because some calculations will depend on converted measures

    The script node is like any other node and can be combined with

    other node types

    It can contain SQL script or a plain view definition select .. asview

  • 7/27/2019 Mock Based Unit Testing

    16/17 2011 SAP AG. All rights reserved. 16

    Performance (brainstorming)

    If we allow arbitrary stacking of views we might end up in very bad

    performance

    A intermediate step might be to add hints to certain model parts

    or to the complete model

    E.g. use the engine hints = deployment type (depending on the hint not all features are

    available)

    SQL deploy as SQL view

    Calc view deploy as calc view

    OLAP deploy as OLAP view

    Alternatively: Convert all view definitions into SQL and delegate the work tothe SQL optimizer

    \\trextest\trex_test\dbaeumges\working\201201_SeoulWS_Optimizer_Semantic_Cal

    cEngine.pptx

    http://localhost/var/www/apps/conversion/tmp/scratch_5//trextest/trex_test/dbaeumges/working/201201_SeoulWS_Optimizer_Semantic_CalcEngine.pptxhttp://localhost/var/www/apps/conversion/tmp/scratch_5//trextest/trex_test/dbaeumges/working/201201_SeoulWS_Optimizer_Semantic_CalcEngine.pptxhttp://localhost/var/www/apps/conversion/tmp/scratch_5//trextest/trex_test/dbaeumges/working/201201_SeoulWS_Optimizer_Semantic_CalcEngine.pptxhttp://localhost/var/www/apps/conversion/tmp/scratch_5//trextest/trex_test/dbaeumges/working/201201_SeoulWS_Optimizer_Semantic_CalcEngine.pptxhttp://localhost/var/www/apps/conversion/tmp/scratch_5//trextest/trex_test/dbaeumges/working/201201_SeoulWS_Optimizer_Semantic_CalcEngine.pptxhttp://localhost/var/www/apps/conversion/tmp/scratch_5//trextest/trex_test/dbaeumges/working/201201_SeoulWS_Optimizer_Semantic_CalcEngine.pptxhttp://localhost/var/www/apps/conversion/tmp/scratch_5//trextest/trex_test/dbaeumges/working/201201_SeoulWS_Optimizer_Semantic_CalcEngine.pptxhttp://localhost/var/www/apps/conversion/tmp/scratch_5//trextest/trex_test/dbaeumges/working/201201_SeoulWS_Optimizer_Semantic_CalcEngine.pptxhttp://localhost/var/www/apps/conversion/tmp/scratch_5//trextest/trex_test/dbaeumges/working/201201_SeoulWS_Optimizer_Semantic_CalcEngine.pptxhttp://localhost/var/www/apps/conversion/tmp/scratch_5//trextest/trex_test/dbaeumges/working/201201_SeoulWS_Optimizer_Semantic_CalcEngine.pptxhttp://localhost/var/www/apps/conversion/tmp/scratch_5//trextest/trex_test/dbaeumges/working/201201_SeoulWS_Optimizer_Semantic_CalcEngine.pptxhttp://localhost/var/www/apps/conversion/tmp/scratch_5//trextest/trex_test/dbaeumges/working/201201_SeoulWS_Optimizer_Semantic_CalcEngine.pptxhttp://localhost/var/www/apps/conversion/tmp/scratch_5//trextest/trex_test/dbaeumges/working/201201_SeoulWS_Optimizer_Semantic_CalcEngine.pptxhttp://localhost/var/www/apps/conversion/tmp/scratch_5//trextest/trex_test/dbaeumges/working/201201_SeoulWS_Optimizer_Semantic_CalcEngine.pptx
  • 7/27/2019 Mock Based Unit Testing

    17/17 2011 SAP AG All rights reserved 17

    Optimizing Example recognize the star schema

    Join

    T6

    T2

    T4

    T5T3

    Data Foundation

    Output

    Customer

    TimeProduct

    Store T8

    T9

    T7

    T3

    T2T4

    T5

    TTIME

    TCUS

    T6

    T8T7

    T9

    TCUSTEXT