Se ha denunciado esta presentación.
Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.
A mocking framework for Java
   http://www.mockito.org/


Richard Paul - http://www.rapaul.com/
State vs Interaction testing

  State testing asserts properties on an object
     e.g. assertEquals(4, item.getCount());
...
Test Doubles

Terminology defined by Gerard Meszaros.
See: http://tinyurl.com/testdoubles

  Test Stub
    Hand coded obje...
Mockito Example

Simple MVC example:

   Item - simple POJO (has a name and ID).
   ItemController - handles requests from...
View Item - using when




Eclipse then generates the viewItem method signature
in ItemController.java
Test Failure




 Then implement viewItem to pass the test
Delete Item - using verify




 Eclipse then generates the deleteItem method
 signature in ItemController.java
Wanted, but not invoked
Exceptional Cases - using thenThrow




Allows simple testing of exceptional cases with similar
syntax to thenReturn .
Initialising mocks in Mockito
                        Allows access to when,
                        verify etc without th...
Default Values
Mockito provides default values (null, 0, empty list, etc) for
method calls that have not been defined. Thi...
More features...
  Verify how many times a method is called. e.g.
     verify(itemService, never()).deleteItem(3);
     ve...
Comments, queries,
suggestions or theories?


Slides + code examples will be up on
        http://www.rapaul.com
Próxima SlideShare
Cargando en…5
×

20

Compartir

Descargar para leer sin conexión

Mocking in Java with Mockito

Descargar para leer sin conexión

Mocking in Java with Mockito

  1. 1. A mocking framework for Java http://www.mockito.org/ Richard Paul - http://www.rapaul.com/
  2. 2. State vs Interaction testing State testing asserts properties on an object e.g. assertEquals(4, item.getCount()); Interaction testing verifies the interactions between objects. e.g. Did my controller correctly call my service. Mockito provides a framework for interactions testing.
  3. 3. Test Doubles Terminology defined by Gerard Meszaros. See: http://tinyurl.com/testdoubles Test Stub Hand coded object used for testing. Mock object (e.g. EasyMock) Expectations configured before calling the method under test. Test Spy (e.g. Mockito) Verification occurs after the method under test has been called.
  4. 4. Mockito Example Simple MVC example: Item - simple POJO (has a name and ID). ItemController - handles requests from the browser. ItemService - service the ItemController delegates to. ItemNotFoundException Following slides detail testing the ItemController using Mockito. Note: the controller is implemented using a Springframework 2.5 annotation style, with annotations omitted for clarity.
  5. 5. View Item - using when Eclipse then generates the viewItem method signature in ItemController.java
  6. 6. Test Failure Then implement viewItem to pass the test
  7. 7. Delete Item - using verify Eclipse then generates the deleteItem method signature in ItemController.java
  8. 8. Wanted, but not invoked
  9. 9. Exceptional Cases - using thenThrow Allows simple testing of exceptional cases with similar syntax to thenReturn .
  10. 10. Initialising mocks in Mockito Allows access to when, verify etc without the ... Mockito.prefix ...
  11. 11. Default Values Mockito provides default values (null, 0, empty list, etc) for method calls that have not been defined. This allows you to define only the interactions that are relevant to your test. See slides by Szczepan Faber http://tinyurl.com/5cxg64 Slides 8-31 Why ignore interactions not directly related to a test case? (slide 29) quot;because aggressive validation makes the tests brittlequot; quot;because I have to fix tests even when the code is not brokenquot; quot;... can increase noisequot; quot;or lead to overspecificationquot;
  12. 12. More features... Verify how many times a method is called. e.g. verify(itemService, never()).deleteItem(3); verify(itemService, atLeastOnce()).deleteItem(3); verify(itemService, times(3)).deleteItem(3); Verify the order calls are made using InOrder Chain subsequent calls to return different values. when(itemService.getItem(3)) .thenReturn(item1) .thenReturn(item2); Argument matchers when(service.get(eq(1), anyFloat()).thenReturn(quot;xquot;); Can mock concrete classes (uses cglib)
  13. 13. Comments, queries, suggestions or theories? Slides + code examples will be up on http://www.rapaul.com
  • AbdelhaqZouha

    Oct. 20, 2020
  • OlgaYaremchuk

    Nov. 27, 2019
  • ManojPatra5

    Nov. 10, 2017
  • glevinsh

    Oct. 8, 2017
  • niravkk

    Apr. 1, 2017
  • miconi

    Mar. 14, 2016
  • ThangLamHuu

    Sep. 22, 2015
  • strongback

    Jul. 21, 2015
  • guilhermeis

    Jun. 13, 2014
  • balladofgale

    Jan. 6, 2014
  • pkoduganty

    Oct. 6, 2013
  • stadlerb

    Jul. 12, 2013
  • stefanteixeira

    May. 31, 2013
  • sandeepmandrumaka

    May. 23, 2013
  • barcel0

    Jan. 23, 2013
  • samchuang

    Sep. 26, 2012
  • colinkuo

    Nov. 10, 2011
  • 777rakesh

    Sep. 15, 2011
  • gordonad

    May. 11, 2010
  • asantos_2000

    Feb. 5, 2010

Vistas

Total de vistas

38.844

En Slideshare

0

De embebidos

0

Número de embebidos

2.458

Acciones

Descargas

670

Compartidos

0

Comentarios

0

Me gusta

20

×