Testing 用Mockito模拟静态方法

Testing 用Mockito模拟静态方法,testing,mockito,powermock,spring-test,Testing,Mockito,Powermock,Spring Test,我正在尝试使用powermock模拟静态方法。 下面是我的代码: public class Helper{ public static User getLoggedInUser(HttpServletRequest request) throws NotFoundException { String access = request.getHeader("Authorization"); if(access == null || access.isEmpty()) {

我正在尝试使用powermock模拟静态方法。 下面是我的代码:

public class Helper{

  public static User getLoggedInUser(HttpServletRequest request) throws NotFoundException {
    String access = request.getHeader("Authorization");
    if(access == null || access.isEmpty()) {
      throw new Exception("Access is null");
    }
    User user = new User();
    return user;

  }

}
这是我调用静态方法getUser的控制器函数:

@RequestMapping(value = "user/userInfo/{Id}", method = RequestMethod.GET, headers = "Accept=application/json")
    public @ResponseBody
    ResultDTO getUser(@PathVariable("Id") Integer Id, HttpServletRequest request) throws NotFoundException, UnauthorizedException {

        Integer userID = -1;

           User user = Helper.getLoggedInUser(request);
           if(user != null){
                userID = user.getUserId();
           }

        //do something
    }
这是我的测试课:

//@RunWith(PowerMockRunner.class)
//@PrepareForTest(Helper.class)
public class CustomerControllerNGTest {

@InjectMocks
    private userController instance = new PaymentCustomerController();
    public PaymentCustomerControllerNGTest() {
    }

    @BeforeClass
    public void setUpClass() throws Exception {
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }

    @BeforeMethod
    public void setUpMethod() throws Exception {

        try{
            MockitoAnnotations.initMocks(this);
        }catch(Exception ex){
           System.out.println(ex.getMessage()); 
        }
        try{
        mockMvc = MockMvcBuilders.standaloneSetup(instance).build();
           // mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
        }catch(Exception ex){
            System.out.println(ex.getMessage());
        }
    }

    @AfterMethod
    public void tearDownMethod() throws Exception {
    }

    @Test
    public void testGetUserInfo() throws Exception {
        User user = new  User();
        user.setUserId(1234);
        HttpServletRequest request = mock(HttpServletRequest.class);

        //this is for the static method
        PowerMockito.mockStatic(Helper.class);
        **PowerMockito.when(Helper.getLoggedInUser(request)).thenReturn(user);**
        //do something


    }

}
现在,每当我执行测试用例,每当它执行用粗体标记的lone时,它进入静态方法并抛出异常“Access is null”,而不是模拟该方法,它都在执行该方法。有什么想法吗? 我还尝试取消这些行的注释:

//@RunWith(PowerMockRunner.class)
//@PrepareForTest(Helper.class)
但还是一样的例外。 谢谢

尝试取消注释:

//@RunWith(PowerMockRunner.class)
//@PrepareForTest(Helper.class) 
和使用

Mockito.when(Helper.getLoggedInUser(request)).thenReturn(user);
我写了一篇博客,其中包含指向GitHub上工作示例的链接。它们使用TestNg而不是JUnit,但这并不重要

编辑


我建议总是使用最新的。旧的组合经常会出现令人困惑的错误。目前最新的组合是Mockito 1.9.5-rc1+,PowerMock 1.5+

您好,谢谢,我这样做了,仍然没有工作:(,有什么想法吗?1.8.5和org.powermock-powermock-api-mockito 1.4.9不明白为什么它在执行静态方法,而我在模拟此方法可以发布您正在使用的mockito和powermock的版本吗?