Mock Based Unit Testing

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17
At a glance
Powered by AI
The key takeaways from the presentation are introducing the Mockito framework for unit and scenario testing, discussing challenges of unit testing and how mocking can help address them, and different categories of modeling elements.

The main features of the Mockito framework discussed are verifying code behaviors, stubbing mock values, argument matchers, verifying number of invocations, stubbing void methods with exceptions, and finding redundant invocations.

One of the challenges of unit testing discussed is the problem of huge unreadable test code. Mocking with Mockito can help address this by generating mocks using reflection and providing various ways to define and validate mock behavior.

Unit/Scenario Testing Approaches

Udayan Datta I079774

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

2011 SAP AG. All rights reserved.

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

2011 SAP AG. All rights reserved.

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 errors o Verification errors should always point stack trace to failed interaction Clean stack trace

2011 SAP AG. All rights reserved.

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.

2011 SAP AG. All rights reserved.

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

2011 SAP AG. All rights reserved.

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 current Mockito - http://blog.griddynamics.com/2010/03/mocking-with-mockito-andpowermock.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

2011 SAP AG. All rights reserved.

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 current Mockito - http://blog.griddynamics.com/2010/03/mocking-with-mockito-andpowermock.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

2011 SAP AG. All rights reserved.

Design of Effective Scenario Test


Test

Workflow

Identify Interactions Create Mocks o Multiple 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

2011 SAP AG. All rights reserved.

An implementation plan for Sprint 2 (SP7) Onwards

TBD

2011 SAP AG. All rights reserved.

10

Q/A/F

Q/A Finalization

Thank You

2011 SAP AG. All rights reserved.

11

Categories of modeling elements


SQL view Joins Unions Basic Semantic (possible also on table and catalog level) Column semantic

Filter
Calculations Group by Order by

Language (SAP, ISO, Locale) Client (MANDT) Amount, Quantity Unit / Currency Time / Date Semantic Description Unit / Currency Hierarchies (requires a key)
Leveled Parent Child Attribute relationships

Advanced Semantic (multi dim) Dimensions Measures Variables for client tools

SQL view ++
Scripts Curr Conv. Parameters

Assignment to dependent columns


Key Non aggregatable column

2011 SAP AG. All rights reserved.

12

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
2011 SAP AG. All rights reserved.

Node Detail

Node columns
13

UI
Analytic view in the new editor with two Multi join / Star join Node
Detail of JoinDIM

Data Found ation

Detail of JoinDF

Data Foundation

Data Found ation

Old
2011 SAP AG. All rights reserved.

New
14

Capabilities
All semantic modeling would be done in the output node? (Maybe also 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 .. as view

2011 SAP AG. All rights reserved.

15

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 to the SQL optimizer \\trextest\trex_test\dbaeumges\working\201201_SeoulWS_Optimizer_Semantic_Cal cEngine.pptx

2011 SAP AG. All rights reserved.

16

Optimizing Example recognize the star schema


Time Product

Output Customer Store

T7 T8 T9

Join

Data Foundation T4 T2 T6 TCUSTEXT TTIME T8

T7

TCUS T4 T5 T3 T6 T5 T2

T9

T3

2011 SAP AG. All rights reserved.

17

You might also like