[JAVA] Use FacesContext as a Mock with PowerMockito


This post is for mocking FacesContext on junit I don't want to create a subclass like FacesContextMock Described as a memorandum how to use PowerMock and do it in the @ Test method


Use the following libraries and versions

  1. java8
  2. junit4.12
  3. PowerMockito (using 1.7.3)


UserAuth Session management of user information


import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

 *User credentials
public class UserAuth implements Serializable {

	/**User ID*/
	private String userId;
	/**Login status*/
	private boolean logined;

	 *Logout process
	 * @return login screen URL
	public String logout() {
		//Discard session information

		//Transit to login screen by redirect
		return "/login.xhtml?faces-redirect=true";

	// ... Setter and Getter


** UserAuth test ** Test the logout process


import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertThat;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

 *UserAuth test class
public class UserAuthTest {

	/**User credentials*/
	private UserAuth userAuth;

	 *Instantiate user credentials class
	public void bofore() {
		userAuth = new UserAuth();

	 *Test logout process<br>
	 *The URL of the logout destination is the login screen.
	public void testLogout() {
		FacesContext context = PowerMockito.mock(FacesContext.class);
		ExternalContext ext = PowerMockito.mock(ExternalContext.class);
		//method setting

		//Check the URL of the logout destination.
		assertThat(userAuth.logout(), containsString("login.xhtml"));


To use PowerMockito @ RunWith (PowerMockRunner.class) in class annotation Add @PrepareForTest (FacesContext.class) to method annotation

FacesContext.getCurrentInstance () is a static method, so mock it with PowerMockito.mockStatic All subsequent instances called will be mocked with mock

After that, when the test is executed, mock is called and processing is performed.

Recommended Posts

Use FacesContext as a Mock with PowerMockito
Use a named format with Ruby's format method
Access with Selenium as a countermeasure for navigator.webdriver
Mock and stub with minitest (use RR, WebMock, MiniTest :: Mock)
Make System.out a Mock with Spock Test Framework
Play down with Raspberry PI4 as a server. Part 2
Considering a property editor to use with SpringToolSuite (STS)
Use java1.7 (zulu7) under a specific directory with jenv
Use Coveralls with GitHub Actions in a Ruby repository
Use ProGuard with Gradle
Use Puphpeteer with Docker
Use XVim2 with Xcode 12.0.1
Mock Enums with PowerMock
Use CentOS with LXD
Use ngrok with Docker
Use webmock with Rspec
GitHub as a storage
Use WebJars with Gradle
Use jlink with gradle
How to request a CSV file as JSON with jMeter
How to use a foreign key with FactoryBot ~ Another solution
How to register as a customer with Square using Tomcat