简体   繁体   中英

Mocking static method inside static method on powermock

I have a static method that looks for the current userSession object and returns a string.

public static String getCurrentUser()
    {
        UserSession userSession = buildUserSession();
        String responseString = userSession.getUsername();
        return responseString;
    }

For that, I call a private static method to instantiate the session object and return it to the first method. The buildUserSession method is reused on many other methods.

private static UserSession buildUserSession()
    {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetails userDetails = (UserDetails) principal;
        String[] rolesArray = ConversionUtils.convertObjectArrayToStringArray(userDetails.getAuthorities().toArray());
        List<String> rolesList = Arrays.asList(rolesArray);
        UserSession userSession = new UserSession();
        userSession.setRoles(rolesList);
        userSession.setUsername(userDetails.getUsername());
        return userSession;
    }

From what I understand to this point, static method mocking is best made with mockito+powermock. Although, I am not managing to make this setup work when I have a static method inside a static method that I want to test. Can you help me with this unit test?

EDIT: I want to test getCurrentUser() and and mock buildUserSession().

@RunWith(PowerMockRunner.class)
@PrepareForTest(SecurityUtils.class)
public class SecurityUtilsTests {

    @Test
    public void getCurrentUserTest() throws Exception {
        // when
        String expected = "testUsername";
        UserSession userSession = new UserSession();
        userSession.setUsername(expected);
        PowerMockito.mockStatic(SecurityUtils.class);
        PowerMockito.when(SecurityUtils.class, "buildUserSession").thenReturn(userSession);
        String actual = SecurityUtils.getCurrentUser();
        assertThat(actual).isEqualTo(expected);
    }
}

Here:

public static String getCurrentUser()

private static UserSession buildUserSession()

And finally:

PowerMockito.mockStatic(SecurityUtils.class);

You tell PowerMockito to mock all static methods within SecurityUtils. And then you want to test what happens when you call one of these methods?

Hint: the moment you go mockStatic(SecurityUtils.class) all static methods on that class are "erased".

Thus: you can't throw away all static methods on a class ... to then assume you can test one of them!

Beyond that: as soon as static gets into your way of doing unit testing, then turning to PowerMock(ito) is the wrong answer. Instead, you should rethink your design so that you can test it without PowerMock(ito).

In other words: learn how to write easy-to-test code. Then you don't need the big PowerMock(ito) hammer to work around the problems that hard-to-test code forces upon you.

I guess you want to test getCurrentUser() and hereby mock buildUserSession() ?

Instead of writing PowerMockito.when(SecurityUtils.class, "buildUserSession").thenReturn(userSession);

you should call

PowerMockito.when(SecurityUtils.buildUserSession()).thenReturn(userSession);

In the when() -clause you have to specify the mocked method as exactly as you would call it without mocking

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM