[java] Mockito를 사용하여 다른 Mock 객체를 리턴하는 방법은?
Mockito는 Java에서 테스트 더블을 생성하고 조작하는 데 사용되는 인기있는 mocking 프레임워크입니다. 다른 Mock 객체를 리턴하기 위해 Mockito를 사용하려면 thenReturn
메서드를 사용할 수 있습니다.
예를 들어, 다음은 Mockito.mock()
메서드를 사용하여 Mock 객체를 생성하고, thenReturn
메서드를 사용하여 Mock 객체를 리턴하는 예제입니다.
import static org.mockito.Mockito.*;
// Mock 객체 생성
SomeClass mockObject = mock(SomeClass.class);
AnotherClass anotherMockObject = mock(AnotherClass.class);
// 다른 Mock 객체를 리턴하도록 설정
when(mockObject.someMethod()).thenReturn(anotherMockObject);
// Mock 객체 사용
AnotherClass result = mockObject.someMethod();
// 확인
assertEquals(anotherMockObject, result);
위의 예제에서 SomeClass
라는 클래스의 someMethod()
메서드가 호출되면, thenReturn
메서드를 사용하여 AnotherClass
의 Mock 객체를 리턴하도록 설정하였습니다. 그런 다음 mockObject.someMethod()
를 호출하여 실제로 Mock 객체가 리턴되는지 확인합니다.
Mockito를 사용하여 다른 Mock 객체를 리턴하는 방법에 대해 알아보았습니다. 이를 통해 테스트할 대상의 동작을 깔끔하게 모의화할 수 있습니다.
더 자세한 내용은 Mockito 공식 문서를 참조하시기 바랍니다.