| |
| |
| |
| |
| package org.mockito; |
|
|
| import org.jspecify.annotations.Nullable; |
| import org.mockito.stubbing.Answer; |
| import org.mockito.stubbing.OngoingStubbing; |
| import org.mockito.stubbing.Stubber; |
| import org.mockito.verification.VerificationMode; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| @SuppressWarnings("unchecked") |
| public class BDDMockito extends Mockito { |
|
|
| |
| |
| |
| |
| public interface BDDMyOngoingStubbing<T> { |
|
|
| |
| |
| |
| |
| BDDMyOngoingStubbing<T> willAnswer(Answer<?> answer); |
|
|
| |
| |
| |
| |
| BDDMyOngoingStubbing<T> will(Answer<?> answer); |
|
|
| |
| |
| |
| |
| BDDMyOngoingStubbing<T> willReturn(@Nullable T value); |
|
|
| |
| |
| |
| |
| @SuppressWarnings({"unchecked", "varargs"}) |
| BDDMyOngoingStubbing<T> willReturn(@Nullable T value, @Nullable T... values); |
|
|
| |
| |
| |
| |
| BDDMyOngoingStubbing<T> willThrow(Throwable... throwables); |
|
|
| |
| |
| |
| |
| BDDMyOngoingStubbing<T> willThrow(Class<? extends Throwable> throwableType); |
|
|
| |
| |
| |
| |
| |
| |
| @SuppressWarnings({"unchecked", "varargs"}) |
| BDDMyOngoingStubbing<T> willThrow( |
| Class<? extends Throwable> throwableType, |
| Class<? extends Throwable>... throwableTypes); |
|
|
| |
| |
| |
| |
| BDDMyOngoingStubbing<T> willCallRealMethod(); |
|
|
| |
| |
| |
| |
| <M> M getMock(); |
| } |
|
|
| private static class BDDOngoingStubbingImpl<T> implements BDDMyOngoingStubbing<T> { |
|
|
| private final OngoingStubbing<T> mockitoOngoingStubbing; |
|
|
| public BDDOngoingStubbingImpl(OngoingStubbing<T> ongoingStubbing) { |
| this.mockitoOngoingStubbing = ongoingStubbing; |
| } |
|
|
| public BDDMyOngoingStubbing<T> willAnswer(Answer<?> answer) { |
| return new BDDOngoingStubbingImpl<T>(mockitoOngoingStubbing.thenAnswer(answer)); |
| } |
|
|
| public BDDMyOngoingStubbing<T> will(Answer<?> answer) { |
| return new BDDOngoingStubbingImpl<T>(mockitoOngoingStubbing.then(answer)); |
| } |
|
|
| public BDDMyOngoingStubbing<T> willReturn(@Nullable T value) { |
| return new BDDOngoingStubbingImpl<T>(mockitoOngoingStubbing.thenReturn(value)); |
| } |
|
|
| public BDDMyOngoingStubbing<T> willReturn(@Nullable T value, @Nullable T... values) { |
| return new BDDOngoingStubbingImpl<T>(mockitoOngoingStubbing.thenReturn(value, values)); |
| } |
|
|
| public BDDMyOngoingStubbing<T> willThrow(Throwable... throwables) { |
| return new BDDOngoingStubbingImpl<T>(mockitoOngoingStubbing.thenThrow(throwables)); |
| } |
|
|
| public BDDMyOngoingStubbing<T> willThrow(Class<? extends Throwable> throwableType) { |
| return new BDDOngoingStubbingImpl<T>(mockitoOngoingStubbing.thenThrow(throwableType)); |
| } |
|
|
| public BDDMyOngoingStubbing<T> willThrow( |
| Class<? extends Throwable> throwableType, |
| Class<? extends Throwable>... throwableTypes) { |
| return new BDDOngoingStubbingImpl<T>( |
| mockitoOngoingStubbing.thenThrow(throwableType, throwableTypes)); |
| } |
|
|
| public BDDMyOngoingStubbing<T> willCallRealMethod() { |
| return new BDDOngoingStubbingImpl<T>(mockitoOngoingStubbing.thenCallRealMethod()); |
| } |
|
|
| public <M> M getMock() { |
| return (M) mockitoOngoingStubbing.getMock(); |
| } |
| } |
|
|
| |
| |
| |
| |
| public static <T> BDDMyOngoingStubbing<T> given(@Nullable T methodCall) { |
| return new BDDOngoingStubbingImpl<T>(Mockito.when(methodCall)); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public static <T> Then<T> then(T mock) { |
| return new ThenImpl<T>(mock); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| public interface Then<T> { |
|
|
| |
| |
| |
| |
| T should(); |
|
|
| |
| |
| |
| |
| T should(VerificationMode mode); |
|
|
| |
| |
| |
| |
| T should(InOrder inOrder); |
|
|
| |
| |
| |
| |
| T should(InOrder inOrder, VerificationMode mode); |
|
|
| |
| |
| |
| |
| void shouldHaveNoMoreInteractions(); |
|
|
| |
| |
| |
| |
| void shouldHaveNoInteractions(); |
| } |
|
|
| private static class ThenImpl<T> implements Then<T> { |
|
|
| private final T mock; |
|
|
| ThenImpl(T mock) { |
| this.mock = mock; |
| } |
|
|
| |
| |
| |
| |
| public T should() { |
| return verify(mock); |
| } |
|
|
| |
| |
| |
| |
| public T should(VerificationMode mode) { |
| return verify(mock, mode); |
| } |
|
|
| |
| |
| |
| |
| public T should(InOrder inOrder) { |
| return inOrder.verify(mock); |
| } |
|
|
| |
| |
| |
| |
| public T should(InOrder inOrder, VerificationMode mode) { |
| return inOrder.verify(mock, mode); |
| } |
|
|
| |
| |
| |
| |
| public void shouldHaveNoMoreInteractions() { |
| verifyNoMoreInteractions(mock); |
| } |
|
|
| |
| |
| |
| |
| public void shouldHaveNoInteractions() { |
| verifyNoInteractions(mock); |
| } |
| } |
|
|
| |
| |
| |
| |
| public interface BDDStubber { |
| |
| |
| |
| |
| BDDStubber willAnswer(Answer<?> answer); |
|
|
| |
| |
| |
| |
| BDDStubber will(Answer<?> answer); |
|
|
| |
| |
| |
| |
| BDDStubber willDoNothing(); |
|
|
| |
| |
| |
| |
| BDDStubber willReturn(Object toBeReturned); |
|
|
| |
| |
| |
| |
| @SuppressWarnings({"unchecked", "varargs"}) |
| BDDStubber willReturn(Object toBeReturned, Object... nextToBeReturned); |
|
|
| |
| |
| |
| |
| BDDStubber willThrow(Throwable... toBeThrown); |
|
|
| |
| |
| |
| |
| BDDStubber willThrow(Class<? extends Throwable> toBeThrown); |
|
|
| |
| |
| |
| |
| @SuppressWarnings({"unchecked", "varargs"}) |
| BDDStubber willThrow( |
| Class<? extends Throwable> toBeThrown, |
| Class<? extends Throwable>... nextToBeThrown); |
|
|
| |
| |
| |
| |
| BDDStubber willCallRealMethod(); |
|
|
| |
| |
| |
| |
| <T> T given(T mock); |
| } |
|
|
| private static class BDDStubberImpl implements BDDStubber { |
|
|
| private final Stubber mockitoStubber; |
|
|
| public BDDStubberImpl(Stubber mockitoStubber) { |
| this.mockitoStubber = mockitoStubber; |
| } |
|
|
| public <T> T given(T mock) { |
| return mockitoStubber.when(mock); |
| } |
|
|
| public BDDStubber willAnswer(Answer<?> answer) { |
| return new BDDStubberImpl(mockitoStubber.doAnswer(answer)); |
| } |
|
|
| public BDDStubber will(Answer<?> answer) { |
| return new BDDStubberImpl(mockitoStubber.doAnswer(answer)); |
| } |
|
|
| public BDDStubber willDoNothing() { |
| return new BDDStubberImpl(mockitoStubber.doNothing()); |
| } |
|
|
| public BDDStubber willReturn(Object toBeReturned) { |
| return new BDDStubberImpl(mockitoStubber.doReturn(toBeReturned)); |
| } |
|
|
| public BDDStubber willReturn(Object toBeReturned, Object... nextToBeReturned) { |
| return new BDDStubberImpl( |
| mockitoStubber.doReturn(toBeReturned).doReturn(nextToBeReturned)); |
| } |
|
|
| public BDDStubber willThrow(Throwable... toBeThrown) { |
| return new BDDStubberImpl(mockitoStubber.doThrow(toBeThrown)); |
| } |
|
|
| public BDDStubber willThrow(Class<? extends Throwable> toBeThrown) { |
| return new BDDStubberImpl(mockitoStubber.doThrow(toBeThrown)); |
| } |
|
|
| public BDDStubber willThrow( |
| Class<? extends Throwable> toBeThrown, |
| Class<? extends Throwable>... nextToBeThrown) { |
| return new BDDStubberImpl(mockitoStubber.doThrow(toBeThrown, nextToBeThrown)); |
| } |
|
|
| public BDDStubber willCallRealMethod() { |
| return new BDDStubberImpl(mockitoStubber.doCallRealMethod()); |
| } |
| } |
|
|
| |
| |
| |
| |
| public static BDDStubber willThrow(Throwable... toBeThrown) { |
| return new BDDStubberImpl(Mockito.doThrow(toBeThrown)); |
| } |
|
|
| |
| |
| |
| |
| public static BDDStubber willThrow(Class<? extends Throwable> toBeThrown) { |
| return new BDDStubberImpl(Mockito.doThrow(toBeThrown)); |
| } |
|
|
| |
| |
| |
| |
| public static BDDStubber willThrow( |
| Class<? extends Throwable> toBeThrown, Class<? extends Throwable>... throwableTypes) { |
| return new BDDStubberImpl(Mockito.doThrow(toBeThrown, throwableTypes)); |
| } |
|
|
| |
| |
| |
| |
| public static BDDStubber willAnswer(Answer<?> answer) { |
| return new BDDStubberImpl(Mockito.doAnswer(answer)); |
| } |
|
|
| |
| |
| |
| |
| public static BDDStubber will(Answer<?> answer) { |
| return new BDDStubberImpl(Mockito.doAnswer(answer)); |
| } |
|
|
| |
| |
| |
| |
| public static BDDStubber willDoNothing() { |
| return new BDDStubberImpl(Mockito.doNothing()); |
| } |
|
|
| |
| |
| |
| |
| public static BDDStubber willReturn(Object toBeReturned) { |
| return new BDDStubberImpl(Mockito.doReturn(toBeReturned)); |
| } |
|
|
| |
| |
| |
| |
| @SuppressWarnings({"unchecked", "varargs"}) |
| public static BDDStubber willReturn(Object toBeReturned, Object... toBeReturnedNext) { |
| return new BDDStubberImpl(Mockito.doReturn(toBeReturned, toBeReturnedNext)); |
| } |
|
|
| |
| |
| |
| |
| public static BDDStubber willCallRealMethod() { |
| return new BDDStubberImpl(Mockito.doCallRealMethod()); |
| } |
| } |
|
|