Issue
- We are encountering issues with jUnit and Liferay Spring MVC portlet. We have SubscriptionUtilTest class and when trying to run it by right click and run as "Junit" then throws error as "java.lang.NoClassDefFoundError" for "Validator.isNotNull" code. I would like to know any reference for using jUnit in Liferay code to validate. My code doesn't have any customization. I am trying to call below statement:
-
if(Validator.isNotNull(screenName)) {
User user = UserLocalServiceUtil.fetchUserByScreenName(20154, screenName);
Validator.isNotNull is not custom code. This is in com.liferay.portal.kernel.util and UserLocalServiceUtil is in com.liferay.portal.kernel.service.
Environment
- Liferay 7.3
Resolution
- Liferay relies on a fully initialized OSGi container for its services. The test code is invoking a static Liferay utility class that relies on an OSGi service tracker during runtime. This tracker locates and uses the specific OSGi service implementation. Since the setup doesn't involve initializing the OSGi container, invoking an out-of-the-box Liferay class (e.g., Validator.isNotNull()) will likely fail.
- Unfortunately, we don't offer specific examples of JUnit test setups that build a complete OSGi container for testing purposes. The best recommendation for the situation is to consider mocking all the Liferay classes and methods your custom code interacts with during the test. This allows you to isolate and test your code without requiring a full Liferay OSGi container.