2013-07-18 2 views

답변

1

당신은 당신이 원하는 일을 할 수 Answer 인터페이스를 구현할 수 있습니다. 여기에 행동에 그것을 보여주는 테스트 케이스는 다음과 같습니다 대신 당신이 원하는 경우에 당신이 정규식 정규 사용할 수 있습니다 contains를 사용

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; 
     } 
    } 

} 

합니다.

관련 문제