【问题标题】:Mocking super method call using easymock/powermock使用 easymock/powermock 模拟超级方法调用
【发布时间】:2018-07-01 08:37:13
【问题描述】:

我将 easymock 与我的 Spring 项目集成并执行了大部分单元测试。但是遇到了无法模拟超级方法的问题。当我运行我的测试类时,它说方法不受支持。

知道出了什么问题,因为我在过去的几天里尽了最大的努力,却无法获得任何关于如何模拟 super 方法的线索。

org.springframework.security.authentication.AuthenticationServiceException: Authentication method not supported: 
    at org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter.attemptAuthentication(UsernamePasswordAuthenticationFilter.java:71)
    at com.dashboard.core.CustomAuthenticationFilter.attemptAuthentication(CustomAuthenticationFilter.java:20)
    at com.dashboard.core.CustomAuthenticationFilterTest.testAttemptAuthentication(CustomAuthenticationFilterTest.java:64)

需要测试的类:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import com.dashboard.domain.ApplicationConstant;

public class CustomAuthenticationFilter extends UsernamePasswordAuthenticationFilter {

    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("j_username");
        String password = request.getParameter("j_password");
        if(isValidUsername(username) && isValidPassword(password) ){
            return super.attemptAuthentication(request, response);
        }
        throw new BadCredentialsException(ApplicationConstant.CREDENTIALS_NOT_FORMAT.getValue());
    }

    private static boolean isValidUsername(String username){
        return !StringUtils.isEmpty(username) && username.matches(ApplicationConstant.USERNAME_PATTERN.getValue());
    }

    private static boolean isValidPassword(String password){
        return !StringUtils.isEmpty(password) && password.matches(ApplicationConstant.PWD_PATTERN.getValue());
    }
}

我的测试课:

import java.util.ArrayList;
import java.util.List;

import org.easymock.EasyMock;
import org.easymock.EasyMockRunner;
import org.easymock.EasyMockSupport;
import org.easymock.Mock;
import org.easymock.TestSubject;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@RunWith(EasyMockRunner.class)
public class CustomAuthenticationFilterTest extends EasyMockSupport{

    @TestSubject
    CustomAuthenticationFilter customAuthenticationFilter  = new CustomAuthenticationFilter();

    @Mock
    UsernamePasswordAuthenticationFilter usernamePasswordAuthenticationFilter;

    @Test
    public void testAttemptAuthentication() throws Exception{   
        MockHttpServletRequest request = new MockHttpServletRequest();
        request.setParameter("j_username", "Sundar1234");
        request.setParameter("j_password", "Sundar1234$$");
        MockHttpServletResponse response = new MockHttpServletResponse();
        SimpleGrantedAuthority authority = new SimpleGrantedAuthority("ROLE_USER");
        List<SimpleGrantedAuthority> updatedAuthorities = new ArrayList<SimpleGrantedAuthority>();
        updatedAuthorities.add(authority);
        User user = new User("Sundar1234", "Sundar1234$$", updatedAuthorities);
        Authentication auth = new UsernamePasswordAuthenticationToken(user,null);
        EasyMock.expect(usernamePasswordAuthenticationFilter.attemptAuthentication(request, response)).andReturn(auth);
        EasyMock.replay(usernamePasswordAuthenticationFilter);
        Assert.assertNotNull(customAuthenticationFilter.attemptAuthentication(request, response));
        EasyMock.verify(usernamePasswordAuthenticationFilter);
    }
}

【问题讨论】:

    标签: spring powermock easymock


    【解决方案1】:

    UsernamePasswordAuthenticationFilter 是一个基类,而不是注入的依赖项。通常,您可以使用partial mock 解决此问题。但是你不能,因为CustomAuthenticationFilter 正在调用super.attemptAuthentication,这不能被嘲笑。

    您的解决方案是:

    1. 完全测试CustomAuthenticationFilter 及其超类
    2. 改用委托模式

    但由于当前 Spring 对此类过滤器的设计,委派似乎很笨拙。所以我认为最好的选择是完全测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-05
      相关资源
      最近更新 更多