• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

powermock and easy mock not working  RSS feed

 
Ranch Hand
Posts: 123
  • 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














 
author and jackaroo
Posts: 12199
280
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: 123
  • 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

 
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
Boost this thread!