Axa Assurance Maroc - Insurer Innovation Award 2024
Unit test your *LocalServiceImpl classes
1. Unit test your *LocalServiceImpl
classes
Professional Service Manager Brazil
Thiago Moreira
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
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. 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
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. 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.”
- Selenium scripts - Too many points to break - complex setup
- 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
- 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
Black magic
- you can do wherever you need with it to
I would say that hard is the learning process.
- need to mock all the LocalService and Persistence interfaces
- one of the great characteristics of Liferay's API is the use of classes with a bunch of static methods.