Introduction

Liferay, JUnit, and Mockito can be used together to build high-quality Java applications. Liferay can be used as a platform for developing and managing web portals and other digital experiences. JUnit can be used for automated testing of the Java code, while Mockito can be used to simulate the behavior of dependencies and collaborators.

To use Liferay, JUnit, and Mockito together, developers can follow these steps:

  • Write Java code that uses Liferay’s APIs and services to create web portals and other digital experiences.
  • Write automated unit tests using JUnit to ensure the correctness and consistency of the Java code.
  • Use Mockito to create mock objects that simulate the behavior of dependencies or collaborators of the Java code.
  • Run the automated unit tests to verify the correctness and consistency of the Java code.

Resources:
It is assumed a Gradle workspace and modules are already set up.
The example below uses versions for Liferay 7.4, Java 11. 

Code:  Liferay-Filter-Junit [https://github.com/Firelay/liferay-filter-junit]

 

 

 

 

How to

To implement JUnit and Mockito into a Liferay module codebase, you can follow these steps:

  1. Add JUnit and Mockito dependencies to your project:

You can add the JUnit and Mockito dependencies to your project by adding the following lines to your build.gradle file:


subprojects {
  plugins.withType(JavaPlugin).whenPluginAdded {
    dependencies {
      testImplementation group: "org.junit.jupiter", name: "junit-jupiter-api", version: "5.9.3"
      testImplementation group: "org.mockito", name: "mockito-core", version: "5.2.0"
      testImplementation group: "org.mockito", name: "mockito-inline", version: "5.2.0"
    }
  }
}

And these dependencies and configuration to the module’s build.gradle file:


dependencies {
   …
      testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine'
      testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api'
      testImplementation group: 'org.mockito', name: 'mockito-inline'
}

configurations {
 testImplementation.extendsFrom compileOnly
}

test {
 useJUnitPlatform()
}
  

Dependencies need to include the engine so the tests are executed.
Including extendsFrom compileOnly configuration ensures that the dependencies are also available for the test.

  1. Create a new test class:

Create a new test class in the src/test/java directory of your Liferay module codebase. This class should have a name that ends with Test, the package should be the same from the class you’re testing. For example, the blade sample for filter-portlet, we will test the ExampleRenderFilter.doFilter:


package com.liferay.blade.samples.portlet.filter;
…
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
…

class ExampleRenderFilterTest {
  @Test
  void testDoFilter() {
   // Mock your values
   // Test your code
   // Verify the results 
  }

}

  
  1. Write test methods:

Inside your test class, write test methods that use JUnit assertions to verify the behavior of your Liferay module code. You can use Mockito to create mock objects and stub their behavior to simulate the behavior of external dependencies.


package com.liferay.sample.portlet.test;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;

import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.junit.Test;

public class SamplePortletTest {
 @Test
 public void testRender() {
  RenderRequest request = mock(RenderRequest.class);
  RenderResponse response = mock(RenderResponse.class);
  FilterChain filterChain = mock(FilterChain.class);

  ExampleRenderFilter exampleRenderFilter = new ExampleRenderFilter();

  exampleRenderFilter.doFilter(request, response);

  Mockito.verify(request).setAttribute("CUSTOM_ATTRIBUTE", "My Custom Attribute Value");
  Mockito.verify(chain).doFilter(request, response);
 }

}
  
  1. Run the tests:

You can run the tests by executing the following command in the terminal:


./gradlew test

This will run all the tests in your module and show the test results in the terminal.

In conclusion, implementing JUnit and Mockito into a Liferay module codebase is a straightforward process. By following the steps outlined in this answer, you can write high-quality tests that verify the behavior of your Liferay module code and simulate the behavior of external dependencies using Mockito.