1
다른 String을 반환하는 클래스가 있습니다. 명시 적으로 각 메서드를 스텁하지 않고도이 클래스의 모든 메서드를 스텁링 할 수 있기를 원합니다. mockito는 정규식으로 스텁을 가지고 있습니까?같은 조롱 된 클래스의 모든 메서드를 스텁하는 방법
감사
다른 String을 반환하는 클래스가 있습니다. 명시 적으로 각 메서드를 스텁하지 않고도이 클래스의 모든 메서드를 스텁링 할 수 있기를 원합니다. mockito는 정규식으로 스텁을 가지고 있습니까?같은 조롱 된 클래스의 모든 메서드를 스텁하는 방법
감사
당신은 당신이 원하는 일을 할 수 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;
}
}
}
합니다.