This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Testing and the fly likes powermock and easy mock not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "powermock and easy mock not working" Watch "powermock and easy mock not working" New topic

powermock and easy mock not working

Darvesh Niz
Ranch Hand

Joined: May 12, 2008
Posts: 120
Hello Guys

i am trying to run powermock and easy mock to mock some final classes which are singleton, i am stuck and also ways error
Here is my maven dependency wiht version. an

The class i am trying to test is AuthUtility which has one class IdentityContext which is singleton and declared as final

public final class AuthUtility {
private static final Logger logger = Logger.getLogger(AuthUtility.class);

private AuthUtility() {

public static UserInfo retrieveUser(String email) {
UserService userService = (UserService) IdentityContext.getInstance().getApplicationContext().getBean("userService");
UserInfo userInfo = new UserInfo();
try {
User user = userService.getUserWithApplicationRoles(email);
return userInfo;
} catch(Exception e) {
logger.error("User: " + email + " retrieve failed. ", e);
return null;

public static boolean isLoggedIn(HttpServletRequest request, HttpServletResponse response) {
IdCookieService cookieService = (IdCookieService) IdentityContext.getInstance().getApplicationContext().getBean("cookieService");
String edmid = UserTrack.getCookieValue(IdentityConstants.COOKIE_EDMID, true, request);
if (edmid != null && !StringUtils.isEmpty(edmid)) {
if (cookieService.isMember(request, response)){
return true;
return false;


Final Class as singleton

The test class

This is the error i get
Cannot subclass final class class

Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11755

You are trying to create your mock using EasyMock - EasyMock cannot override static or final. This is where PowerMock comes in - it can override them. Change how you are creating your mocked instance.

The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Darvesh Niz
Ranch Hand

Joined: May 12, 2008
Posts: 120

Notice the
@RunWith(PowerMockRunner.class) annotation at the beginning of the class.
Someone suggested me that i dont have to explicitly call powermock.create mock and easy mock classes will be able to create an instance of final class. Dont know how true it is

anyway i try these statement also in the setUp method identityContext = PowerMock.createMock(IdentityContext.class)

and still the same result

enric jaen

Joined: Oct 15, 2010
Posts: 27

With junit it's working for me. I extend from TestCase instead of PowerMockTestCase. Not sure if this is relevant.

I agree. Here's the link:
subject: powermock and easy mock not working
jQuery in Action, 3rd edition