• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
2013 12-03-gs-unit-tests
 

2013 12-03-gs-unit-tests

on

  • 183 views

 

Statistics

Views

Total Views
183
Views on SlideShare
183
Embed Views
0

Actions

Likes
0
Downloads
2
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    2013 12-03-gs-unit-tests 2013 12-03-gs-unit-tests Presentation Transcript

    • Unit  test  your  *LocalServiceImpl Thiago Moreira Professional Services Manager ! Fernando Tadashi Akimoto Consultant
    • Content • Why  test  LocalServiceImpl  classes?   • Past  attempts  to  test   • Our  findings:  Mock  API   • How  to  set  up  unit  tests   • Sample  application   • Mocking  objects   • Mocking  static  methods   • Mocking  static  void  methods 2
    • Why  unit  test  *LocalServiceImpl  classes? • Reduce  bugs  in  new  features   • Reduce  bugs  in  existing  features   • Improve  Design   • Allow  Refactoring 3
    • Past  attempts  to  test • Very  difficult  set  up  portal  context   • Complex  object  dependencies   Example: MyPersistence persistence = new MyPersistenceImpl();// Fail!!! MyLocalServiceImpl service = new MyLocalServiceIml(); service.setMyPersistence(persistence); 4
    • Our  findings:  Mock  API EasyMock   PowerMock   “EasyMock  provides  Mock  Objects   for  interfaces  (and  objects   through  the  class  extension)  by   generating  them  on  the  fly  using   Java's  proxy  mechanism.  Due  to   EasyMock's  unique  style  of   recording  expectations,  most   refactorings  will  not  affect  the   Mock  Objects.  So  EasyMock  is  a   perfect  fit  for  Test-­‐Driven   Development.” “PowerMock  is  a  framework  that   extend  other  mock  libraries  such   as  EasyMock  with  more  powerful   capabilities.  PowerMock  uses  a   custom  classloader  and  bytecode   manipulation  to  enable  mocking   of  static  methods,  constructors,   final  classes  and  methods,  private   methods,  removal  of  static   initializers  and  more.”   5
    • Sample  Application   • Code  available  in:   http://git.io/uC5OmQ   6
    • How  to  set  up  unit  tests   • Add  maven  dependencies:   <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <version>${powermock.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-easymock</artifactId> <version>${powermock.version}</version> <scope>test</scope> </dependency> *${powermock.version} - 1.5.1 (latest version) 7
    • Unit  tests  steps Create  Mock Set   expectations Set  ready   (replay) Test Verify 8
    • Mocking  Objects ! CounterLocalService counterLocalService = createNiceMock(CounterLocalService.class); ! ! expected( counterLocalService.increment(EasyMock.anyObject()) ).andReturn(testId); ! ! replay(counterLocalService); ! ! Car car = service.addCar(brand, model, manufacturingYear, fuelType, …); ! long newTestId = counterLocalService.increment(Car.class.getName()); ! ! Create   Mock Set   expectati ons Set   ready   (replay) Test verify(counterLocalService); Verify 9
    • Mocking  static  methods ! PowerMock.mockStatic(PortalClassInvoker.class); ! ! expect( PortalClassInvoker.invoke(…) ).andReturn(12); ! ! PowerMock.replay(PortalClassInvoker.class); ! ! Object object = PortalClassInvoker.invoke(…); Create   Mock Set   expectati ons Set   ready   (replay) Test ! ! PowerMock.verify(PortalClassInvoker.class); Verify ! 10
    • Mocking  static  void  methods   ! PowerMock.mockStatic(WorkflowHandlerRegistryUtil.class); ! ! WorkflowHandlerRegistryUtil.startWorkflowInstance(…); ! ! PowerMock.replay(WorkflowHandlerRegistryUtil.class); ! ! Car car = service.addCar(brand, model, manufacturingYear, …); ! ! ! Create   Mock Set   expectati ons Set   ready   (replay) Test WorkflowHandlerRegistryUtil.startWorkflowInstance(…); PowerMock.verify(WorkflowHandlerRegistryUtil.class); Verify ! 11
    • References • Top  12  Reasons  to  Write  Unit  Tests  -­‐  http:// www.onjava.com/pub/a/onjava/2003/04/02/ javaxpckbk.html   • Easy  Mock  -­‐  http://easymock.org   • Power  Mock  -­‐  https://code.google.com/p/ powermock 12
    • Questions? 13
    • Thank  you  guys! Thiago  Moreira   Professional  Services  Manager   thiago.moreira@liferay.com   ! Fernando  Tadashi  Akimoto   Consultant   fernando.akimoto@liferay.com 14