Reputation: 27
Can you please help me to define the PowerMock for that code:
String role = "ROLE_WARP_PUBLISH_PRIVATE";
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
for (GrantedAuthority auth : authorities)
{
if(auth.getAuthority().equals(role))
{
if (publishCheckbox == true)
{
getScenarioService().makePublic(scenarioVersionId);
}
}
}
So I know how that I have to mock the getContext()
but I cannot return something when this method is called so I have a totally blackout how I can mock those few lines.
Thanks for helping
Upvotes: 2
Views: 396
Reputation: 140457
My recommendation: forget about using PowerMock.
If you have to mock static methods, then build your own little wrapper class around that. Then, for testing, your wrapper can return something you control; and for production usage; your wrapper just calls the static method.
PowerMock looks like the solution to many problems; but rather sooner than later, it will be the cause of much more problems.
Seriously: if your design can only be tested with PowerMock, this is very often a clear indication that your design is bad. So: focus on reworking your code under test; instead of investing time into a tool like PowerMock that does more harm than good.
I have spent countless hours trying to resolve PowerMock problems; and since instead started to write "better to test" production code ... I have written hundreds or thousands of tests without ever needing PowerMock again.
Upvotes: 2