aspose file tools*
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 Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "powermock and easy mock not working" Watch "powermock and easy mock not working" New topic
Author

powermock and easy mock not working

Darvesh Niz
Ranch Hand

Joined: May 12, 2008
Posts: 119
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);
userInfo.setUser(user);
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 com.xxx.identity.IdentityContext














Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11481
    
  94

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: 119
Thanks

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
Greenhorn

Joined: Oct 15, 2010
Posts: 25

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: http://aspose.com/file-tools
 
subject: powermock and easy mock not working