2013-07-18 59 views

回答

1

您可以實現Answer接口,做你想做的。這是一個測試案例顯示在用行動表明:

package com.sandbox; 

import org.junit.Test; 
import org.mockito.invocation.InvocationOnMock; 
import org.mockito.stubbing.Answer; 

import static org.junit.Assert.assertEquals; 
import static org.mockito.Mockito.mock; 

public class SandboxTest { 

    @Test 
    public void testMocking() { 
     Foo foo = mock(Foo.class, new Answer() { 
      @Override 
      public Object answer(InvocationOnMock invocation) throws Throwable { 
       String name = invocation.getMethod().getName(); 
       if (name.contains("get")) { 
        return "this is a getter"; 
       } 
       return null; 
      } 
     }); 

     assertEquals("this is a getter", foo.getY()); 
     assertEquals("this is a getter", foo.getX()); 
    } 

    public static class Foo { 
     private String x; 
     private String y; 

     public String getX() { 
      return x; 
     } 

     public void setX(String x) { 
      this.x = x; 
     } 

     public String getY() { 
      return y; 
     } 

     public void setY(String y) { 
      this.y = y; 
     } 
    } 

} 

而不是使用contains如果你願意,你可以使用正則表達式匹配的。