Unit test your *LocalServiceImpl
classes
Professional Service Manager Brazil
Thiago Moreira
Content
●
Why test LocalServiceImpl classes?
●
Past attempts to test
●
Easy things to test
●
What is EasyMock and PowerMoc...
Why unit test *LocalServiceImpl classes?
Past attempts to test *LSImpl
We didn't automate tests at all
Past attempts to test *LSImpl
Integrated tests
Past attempts to test *LSImpl
●
Implemented all interfaces that were dependencies in a
given LocalServiceImpl
●
MockUserLo...
Sample application
●
Project based on Maven
●
One entity called BeerDrinker (name and alcoholLevel fields)
●
Workflow enab...
Easy things to test first
Easy things to test first
Easy things to test first
Refactoring
Next step
●
Increment complexity
●
EasyMock/PowerMock
What is EasyMock?
“EasyMock provides Mock Objects for interfaces (and objects
through the class extension) by generating t...
What is PowerMock?
“PowerMock is a framework that extend other mock libraries
such as EasyMock with more powerful capabili...
What is EasyMock/PowerMock?
My opinion: EasyMock and PowerMock are the swiss army
knife for unit testing.
What is EasyMock/PowerMock?
Set
expectations
Create Mock VerifyTest
Set ready
(replay)
Complex things to test
Complex things to test
CounterLocalService
Create the mock
CounterLocalService counterLocalService =
createNiceMock(CounterLocalService.class);
Set expectations
String className = BeerDrinker.class.getName();
expect(counterLocalService.increment(className)).andRe
tu...
Switch to replay mode
replay(counterLocalService);
Set the mock object in impl class
impl.setCounterLocalService(counterLocalService);
Verify
verify(counterLocalService);
Mocking static methods
Enabling workflow
Adding annotations
Testing workflow
Challenges
●
Culture
●
Lack of documentation/articles
●
Verbosity
thiago.moreira@liferay.com
@tmoreira2020
thiago.moreira@liferay.com
@tmoreira2020
Thank you
Upcoming SlideShare
Loading in …5
×

Unit test your *LocalServiceImpl classes

1,416 views

Published on

Published in: Technology, Education
  • Be the first to comment

Unit test your *LocalServiceImpl classes

  1. 1. Unit test your *LocalServiceImpl classes Professional Service Manager Brazil Thiago Moreira
  2. 2. Content ● Why test LocalServiceImpl classes? ● Past attempts to test ● Easy things to test ● What is EasyMock and PowerMock? ● Complex things to test ● Mocking static methods
  3. 3. Why unit test *LocalServiceImpl classes?
  4. 4. Past attempts to test *LSImpl We didn't automate tests at all
  5. 5. Past attempts to test *LSImpl Integrated tests
  6. 6. Past attempts to test *LSImpl ● Implemented all interfaces that were dependencies in a given LocalServiceImpl ● MockUserLocalService, MockPortal, MockIndexerRegistry ● Created a project called portal-test
  7. 7. Sample application ● Project based on Maven ● One entity called BeerDrinker (name and alcoholLevel fields) ● Workflow enabled ● Class in test BeerDrinkerLocalServiceImpl referenced as just impl. ● Test class BeerDrinkerLocalServiceImplTest ● Code available in github.com/tmoreira2020/liferay-service-builder-unit-testable
  8. 8. Easy things to test first
  9. 9. Easy things to test first
  10. 10. Easy things to test first
  11. 11. Refactoring
  12. 12. Next step ● Increment complexity ● EasyMock/PowerMock
  13. 13. What is EasyMock? “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.”
  14. 14. What is 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.”
  15. 15. What is EasyMock/PowerMock? My opinion: EasyMock and PowerMock are the swiss army knife for unit testing.
  16. 16. What is EasyMock/PowerMock? Set expectations Create Mock VerifyTest Set ready (replay)
  17. 17. Complex things to test
  18. 18. Complex things to test
  19. 19. CounterLocalService
  20. 20. Create the mock CounterLocalService counterLocalService = createNiceMock(CounterLocalService.class);
  21. 21. Set expectations String className = BeerDrinker.class.getName(); expect(counterLocalService.increment(className)).andRe turn(beerDrinkerId);
  22. 22. Switch to replay mode replay(counterLocalService);
  23. 23. Set the mock object in impl class impl.setCounterLocalService(counterLocalService);
  24. 24. Verify verify(counterLocalService);
  25. 25. Mocking static methods
  26. 26. Enabling workflow
  27. 27. Adding annotations
  28. 28. Testing workflow
  29. 29. Challenges ● Culture ● Lack of documentation/articles ● Verbosity
  30. 30. thiago.moreira@liferay.com @tmoreira2020 thiago.moreira@liferay.com @tmoreira2020 Thank you

×