2013 12-03-gs-unit-tests
-
Upload
fernando-tadashi-akimoto -
Category
Technology
-
view
108 -
download
0
description
Transcript of 2013 12-03-gs-unit-tests
![Page 1: 2013 12-03-gs-unit-tests](https://reader033.fdocuments.in/reader033/viewer/2022051412/54b502b44a7959c6378b461a/html5/thumbnails/1.jpg)
Unit test your *LocalServiceImpl
Thiago Moreira Professional Services Manager !Fernando Tadashi Akimoto Consultant
![Page 2: 2013 12-03-gs-unit-tests](https://reader033.fdocuments.in/reader033/viewer/2022051412/54b502b44a7959c6378b461a/html5/thumbnails/2.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051412/54b502b44a7959c6378b461a/html5/thumbnails/3.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051412/54b502b44a7959c6378b461a/html5/thumbnails/4.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051412/54b502b44a7959c6378b461a/html5/thumbnails/5.jpg)
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 7: 2013 12-03-gs-unit-tests](https://reader033.fdocuments.in/reader033/viewer/2022051412/54b502b44a7959c6378b461a/html5/thumbnails/7.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051412/54b502b44a7959c6378b461a/html5/thumbnails/8.jpg)
Unit tests steps
���8
Create MockSet
expectationsSet ready (replay)
Test Verify
![Page 9: 2013 12-03-gs-unit-tests](https://reader033.fdocuments.in/reader033/viewer/2022051412/54b502b44a7959c6378b461a/html5/thumbnails/9.jpg)
!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](https://reader033.fdocuments.in/reader033/viewer/2022051412/54b502b44a7959c6378b461a/html5/thumbnails/10.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051412/54b502b44a7959c6378b461a/html5/thumbnails/11.jpg)
!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](https://reader033.fdocuments.in/reader033/viewer/2022051412/54b502b44a7959c6378b461a/html5/thumbnails/12.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022051412/54b502b44a7959c6378b461a/html5/thumbnails/13.jpg)
Questions?
���13
![Page 14: 2013 12-03-gs-unit-tests](https://reader033.fdocuments.in/reader033/viewer/2022051412/54b502b44a7959c6378b461a/html5/thumbnails/14.jpg)
Thank you guys!
Thiago Moreira Professional Services Manager
Fernando Tadashi Akimoto Consultant
���14