unit test your *localserviceimpl classes
TRANSCRIPT
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 PowerMock?
Complex things to test
Mocking static methods
Why unit test *LocalServiceImpl classes?
The flux capacitor
Past attempts to test *LSImpl
We didn't automate tests at all
Bugs in production
Past attempts to test *LSImpl
Integrated tests
- Selenium scripts- Too many points to break- complex setup
Past attempts to test *LSImpl
Implemented all interfaces that were dependencies in a given LocalServiceImpl
MockUserLocalService, MockPortal, MockIndexerRegistry
Created a project called portal-test
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
Easy things to test first
- start doing easy thing- to no be demotivated- my girlfriend is driving me crazy to buy a dog and we live in an apartment. This won't work except if the dogs helps me to test
Easy things to test first
- so what is easy to test in a *LocalServiceImpl class?- parameters- don't need fancy test frameworks to test it- plain junit do the job
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 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.
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.
Black magic
What is EasyMock/PowerMock?
My opinion: EasyMock and PowerMock are the swiss army knife for unit testing.
- you can do wherever you need with it to
What is EasyMock/PowerMock?
Set expectationsCreate MockVerify
Test
Set ready (replay)
Complex things to test
I would say that hard is the learning process.
Complex things to test
- need to mock all the LocalService and Persistence interfaces
CounterLocalService
Create the mock
CounterLocalService counterLocalService = createNiceMock(CounterLocalService.class);
Set expectations
String className = BeerDrinker.class.getName();expect(counterLocalService.increment(className)).andReturn(beerDrinkerId);
Switch to replay mode
replay(counterLocalService);
Set the mock object in impl class
impl.setCounterLocalService(counterLocalService);
Verify
verify(counterLocalService);
Mocking static methods
- one of the great characteristics of Liferay's API is the use of classes with a bunch of static methods.
Enabling workflow
Adding annotations
Testing workflow
Challenges
Culture
Lack of documentation/articles
Verbosity
[email protected]@tmoreira2020
[email protected]@tmoreira2020
Thank you
Click to edit the title text formatClick to edit Master title style
Click to edit the outline text formatSecond Outline LevelThird Outline LevelFourth Outline LevelFifth Outline LevelSixth Outline Level
Seventh Outline LevelClick to edit Master text styles
Click to edit the outline text formatSecond Outline LevelThird Outline LevelFourth Outline LevelFifth Outline LevelSixth Outline Level
Seventh Outline LevelClick to edit Master text styles
Click to edit the title text formatClick to edit Master title style
Click to edit the outline text formatSecond Outline LevelThird Outline LevelFourth Outline LevelFifth Outline LevelSixth Outline Level
Seventh Outline LevelClick to edit Master text stylesSecond levelThird level