14
Unit test your *LocalServiceImpl Thiago Moreira Professional Services Manager Fernando Tadashi Akimoto Consultant

2013 12-03-gs-unit-tests

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: 2013 12-03-gs-unit-tests

Unit  test  your  *LocalServiceImpl

Thiago Moreira Professional Services Manager !Fernando Tadashi Akimoto Consultant

Page 2: 2013 12-03-gs-unit-tests

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

Page 3: 2013 12-03-gs-unit-tests

Why  unit  test  *LocalServiceImpl  classes?• Reduce  bugs  in  new  features  

• Reduce  bugs  in  existing  features  

• Improve  Design  

• Allow  Refactoring

���3

Page 4: 2013 12-03-gs-unit-tests

Past  attempts  to  test• Very  difficult  set  up  portal  context  

• Complex  object  dependencies  

���4

Example:

MyPersistence persistence = new MyPersistenceImpl();// Fail!!!

MyLocalServiceImpl service = new MyLocalServiceIml();

service.setMyPersistence(persistence);

Page 5: 2013 12-03-gs-unit-tests

Our  findings:  Mock  APIEasyMock  

“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.”

���5

PowerMock  

“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.”  

Page 6: 2013 12-03-gs-unit-tests

• Code  available  in:  

http://git.io/uC5OmQ  

���6

Sample  Application  

Page 7: 2013 12-03-gs-unit-tests

How  to  set  up  unit  tests  • Add  maven  dependencies:  

���7

<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)

Page 8: 2013 12-03-gs-unit-tests

Unit  tests  steps

���8

Create  MockSet  

expectationsSet  ready  (replay)

Test Verify

Page 9: 2013 12-03-gs-unit-tests

!CounterLocalService counterLocalService = createNiceMock(CounterLocalService.class);

!!expected( counterLocalService.increment(EasyMock.anyObject()) ).andReturn(testId);

!!replay(counterLocalService);

!!Car car = service.addCar(brand, model, manufacturingYear, fuelType, …);

!!!verify(counterLocalService);

���9

Mocking  ObjectsCreate  Mock

Set  expectati

ons

Set  ready  (replay)

Test

Verify

long newTestId = counterLocalService.increment(Car.class.getName());

Page 10: 2013 12-03-gs-unit-tests

Mocking  static  methods

���10

!PowerMock.mockStatic(PortalClassInvoker.class);

!!expect( PortalClassInvoker.invoke(…) ).andReturn(12);

!!PowerMock.replay(PortalClassInvoker.class);

!!Object object = PortalClassInvoker.invoke(…);

!!PowerMock.verify(PortalClassInvoker.class);

!

Create  Mock

Set  expectati

ons

Set  ready  (replay)

Test

Verify

Page 11: 2013 12-03-gs-unit-tests

!PowerMock.mockStatic(WorkflowHandlerRegistryUtil.class);

!!WorkflowHandlerRegistryUtil.startWorkflowInstance(…);

!!PowerMock.replay(WorkflowHandlerRegistryUtil.class);

!!Car car = service.addCar(brand, model, manufacturingYear, …);

!!!PowerMock.verify(WorkflowHandlerRegistryUtil.class);

!

���11

Mocking  static  void  methods  

Create  Mock

Set  expectati

ons

Set  ready  (replay)

Test

Verify

WorkflowHandlerRegistryUtil.startWorkflowInstance(…);

Page 12: 2013 12-03-gs-unit-tests

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

Page 13: 2013 12-03-gs-unit-tests

Questions?

���13

Page 14: 2013 12-03-gs-unit-tests

Thank  you  guys!

Thiago  Moreira  Professional  Services  Manager  

[email protected]  !

Fernando  Tadashi  Akimoto  Consultant  

[email protected]

���14