• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

powermock and easy mock not working

 
Darvesh Niz
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 11867
194
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Darvesh Niz
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic