【问题标题】:PowerMock for SecurityContextHolder not workingSecurityContextHolder 的 PowerMock 不起作用
【发布时间】:2015-09-29 05:05:21
【问题描述】:

对于从 SecurityContextHolder 中检索名称的以下逻辑,无法找到完成模拟测试的解决方案。

String userName = SecurityContextHolder.getContext()
            .getAuthentication().getName();

模拟测试:

@RunWith(PowerMockRunner.class)
@PrepareForTest(CustomAuthenticationSuccessHandlerTest.class)
public class CustomAuthenticationSuccessHandlerTest {

private CustomAuthenticationSuccessHandler successHandler;

    @Before
    public void setUp() {
        successHandler = new CustomAuthenticationSuccessHandler();
    }

    // https://github.com/pkainulainen/spring-mvc-test-examples/blob/master/security-url-based/src/test/java/net/petrikainulainen/spring/testmvc/security/util/SecurityContextUtilTest.java

    @Test
    public void test() {

         HttpServletRequest mockRequest = PowerMockito.mock(HttpServletRequest.class);
         HttpServletResponse mockResponse = PowerMockito.mock(HttpServletResponse.class);

        PowerMockito.mockStatic(SecurityContextHolder.class);
        SecurityContext mockSecurityContext = PowerMockito.mock(SecurityContext.class);
        Authentication authenticationMock = PowerMockito.mock(Authentication.class);
        PowerMockito.when(SecurityContextHolder.getContext()).thenReturn(mockSecurityContext);
        PowerMockito.when(mockSecurityContext.getAuthentication()).thenReturn(authenticationMock);
        PowerMockito.when(authenticationMock.getName()).thenReturn("userName");

...

Maven依赖确保

<!-- Mock Objects Library for Java -->
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.10.19</version>
            <scope>test</scope>
        </dependency>

<powermock.version>1.6.2</powermock.version>

兼容..但响应仍然不正确。

【问题讨论】:

  • 你想测试什么?

标签: spring spring-security mockito powermockito


【解决方案1】:

我看到你的代码,感觉在mockStatic 中对于SecurityContextHolder.class 有一些东西。

编辑:我还看到您使用PrepareForTest(YourTestClass),但我不明白您为什么要这样做。 @PrepareForTest 语句仅用于类(而不是对象!),因此您需要在实例化它们之前对其进行更改。例如。当您更改该类的所有实例的某些代码的行为(例如静态...)时,您可能希望 PrepareForTest 您的 SecurityContextHolder.class 类。

你可以试试这个:

@RunWith(PowerMockRunner.class)
@PrepareForTest(SecurityContextHolder.class)
public testClass{         
    @Test
    public void testSomething(){
        // this prepares the class; all static methods get a default return value (in this case type Object is returned and the default returned object is null)
        PowerMockito.mockStatic(SecurityContextHolder.class, new Answer<Object>() 
        {
            @Override
            public Object answer(InvocationOnMock invocation) throws Throwable {
                return null;
            }
        });

        //Now the class is prepared, we can just change the behaviour of the static method in the way we would like.
        String in = "example input string";
        String out = "example output string";        

        PowerMockito.when(SecurityContextHolder.getContext()).thenReturn(out);
    }
    //..body
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多