Initializing ApplicationUser in Unit Tests

I am testing a class in jira plugin and get NullPointerException on this line in the tested class:

permissionManager.hasPermission(ProjectPermissions.CREATE_ISSUES, project, JIRA_USER)

because:

public static final ApplicationUser JIRA_USER = ComponentAccessor.getComponent(UserManager.class).getUserByKey("jira");

assigns null to JIRA_USER.
How Can I initialize a MockedUser in tests to make that JIRA_USER in main code not null?

I have tried like this:

String name = "lalala";
String userEmail = "example@exaple.com";
ApplicationUser user = new MockApplicationUser(name, name, name, userEmail);
new MockComponentWorker().addMock(ApplicationUser.class, user).init();

but it does not work.

This is how it can be done:

Replace ComponentAccessor.getComponent(UserManager.class) with injecting UserManager and inject following:

MockUserManager userManager = new MockUserManager();
userManager.addUser(applicationUser);
1 Like