How to verify multiple method calls with different params

前端 未结 7 1675
自闭症患者
自闭症患者 2020-12-13 08:14

I have the following method that I wish to verify behaviour on.

public void methodToTest(Exception e, ActionErrors errors) {
    ...

    errors.add("exc         


        
7条回答
  •  伪装坚强ぢ
    2020-12-13 08:23

    you probably have a problem in your code. Because as a matter of fact you actually write this code:

    Map map = mock(Map.class);
    
    map.put('a', "a");
    map.put('b', "b");
    map.put('c', "c");
    
    verify(map).put(eq('c'), anyString());
    verify(map).put(eq('a'), anyString());
    verify(map).put(eq('b'), anyString());
    

    Note the first verify is not even in order in regard of the actual invocations.

    Also, I would recommand you to actually don't mock types you don't own, eg the struts type.

    [EDIT @Brad]

    After running Brice's code (above) in my IDE I can see that I have used ActionError instead of ActionMessage, so that is why my verify() was not matching. The error message I initially posted was misleading me into thinking it was the first argument that was not matching. It turns out it was the second argument.

    So the answer to my question is

    /** 
     * note that ActionMessageFactory.createErrorMessage() returns ActionMessage
     * and ActionError extends ActionMessage
     */
    verify(errors).add(eq("exception.message"), any(ActionMessage.class));
    verify(errors).add(eq("exception.detail"), any(ActionMessage.class));
    

提交回复
热议问题