모키토 모조 오브젝트가 다음에 호출되었을 때 다른 것을 반환하도록 지시하려면 어떻게 해야 합니까?
이렇게 클래스 레벨에서 정적 변수로 모의 객체를 만듭니다.한 번의 테스트에서 나는Foo.someMethod()
특정 값을 반환하는 경우 다른 테스트에서는 다른 값을 반환해야 합니다.문제는 제대로 작동하려면 모크를 다시 조립해야 할 것 같다는 것입니다.저는 모크를 재구축하는 것을 피하고 각 테스트에서 같은 오브젝트를 사용하고 싶습니다.
class TestClass {
private static Foo mockFoo;
@BeforeClass
public static void setUp() {
mockFoo = mock(Foo.class);
}
@Test
public void test1() {
when(mockFoo.someMethod()).thenReturn(0);
TestObject testObj = new TestObject(mockFoo);
testObj.bar(); // calls mockFoo.someMethod(), receiving 0 as the value
}
@Test
public void test2() {
when(mockFoo.someMethod()).thenReturn(1);
TestObject testObj = new TestObject(mockFoo);
testObj.bar(); // calls mockFoo.someMethod(), STILL receiving 0 as the value, instead of expected 1.
}
}
두 번째 테스트에서는 testObj.bar()이 호출되었을 때 값으로 0이 표시됩니다.이 문제를 해결하는 가장 좋은 방법은 무엇입니까?다른 모의실험을 사용할 수 있다는 것을 알고 있습니다.Foo
그러나 각 테스트에서 여러 요청을 체인으로 분리해야 합니다.mockFoo
각 테스트마다 체인을 해야 한다는 뜻이죠
연속 콜을 스터브할 수도 있습니다(2.8.9 api의 #10).이 경우 복수의 then Return 콜 또는 복수의 파라미터(varargs)를 가진1개의 return 콜을 사용합니다.
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.junit.Before;
import org.junit.Test;
public class TestClass {
private Foo mockFoo;
@Before
public void setup() {
setupFoo();
}
@Test
public void testFoo() {
TestObject testObj = new TestObject(mockFoo);
assertEquals(0, testObj.bar());
assertEquals(1, testObj.bar());
assertEquals(-1, testObj.bar());
assertEquals(-1, testObj.bar());
}
private void setupFoo() {
mockFoo = mock(Foo.class);
when(mockFoo.someMethod())
.thenReturn(0)
.thenReturn(1)
.thenReturn(-1); //any subsequent call will return -1
// Or a bit shorter with varargs:
when(mockFoo.someMethod())
.thenReturn(0, 1, -1); //any subsequent call will return -1
}
}
무언가를 반환하기 위해 검색한 모든 사용자 및 다른 콜 송출 예외:
when(mockFoo.someMethod())
.thenReturn(obj1)
.thenReturn(obj2)
.thenThrow(new RuntimeException("Fail"));
또는
when(mockFoo.someMethod())
.thenReturn(obj1, obj2)
.thenThrow(new RuntimeException("Fail"));
먼저, 모의 정전기 방지 장치를 만들지 마십시오.개인 분야로 해 주세요.셋업 클래스를@Before
것은 아니다.@BeforeClass
많이 운영되긴 하지만 싸요.
두 번째, 지금 가지고 있는 방법은 모의고사가 시험에 따라 다른 것을 돌려주도록 하는 올바른 방법입니다.
또는 더 깔끔한 경우도 있습니다.
when(mockFoo.someMethod()).thenReturn(obj1, obj2);
when() 메서드 대신 spy() 및 doReturn()을 사용하는 모든 사용자의 경우:
콜마다 다른 오브젝트를 반환하기 위해 필요한 것은 다음과 같습니다.
doReturn(obj1).doReturn(obj2).when(this.spyFoo).someMethod();
.
클래식 모크의 경우:
when(this.mockFoo.someMethod()).thenReturn(obj1, obj2);
또는 예외가 발생하는 경우:
when(mockFoo.someMethod())
.thenReturn(obj1)
.thenThrow(new IllegalArgumentException())
.thenReturn(obj2, obj3);
언급URL : https://stackoverflow.com/questions/4216569/how-to-tell-a-mockito-mock-object-to-return-something-different-the-next-time-it
'sourcecode' 카테고리의 다른 글
jQuery를 사용한 addClass/removeClass 애니메이션 생성 (0) | 2022.11.16 |
---|---|
Content Security Policy(CSP; 콘텐츠보안정책)의 구조 (0) | 2022.11.16 |
IntelliJ Idea 프로젝트 트리에서 컴파일 오류를 즉시 확인하는 방법 (0) | 2022.11.16 |
WebSphere MQ 또는 Tibco Rendezvous와 같은 메시징 솔루션 대신 액터를 사용해야 하는 경우 (0) | 2022.11.16 |
플러그인 'org.springframework'입니다.boot: spring-boot-maven-boot:'를 찾을 수 없음 (0) | 2022.11.16 |