Java Code Examples for org.mockito.internal.util.MockUtil#isMock()

The following examples show how to use org.mockito.internal.util.MockUtil#isMock() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ValidatingPoolableLdapConnectionFactoryTest.java    From directory-ldap-api with Apache License 2.0 7 votes vote down vote up
private static final LdapConnection verify( LdapConnection connection, VerificationMode mode )
{
    if ( MockUtil.isMock( connection ) )
    {
        return org.mockito.Mockito.verify( connection, mode );
    }
    else
    {
        if ( connection instanceof Wrapper )
        {
            @SuppressWarnings("unchecked")
            LdapConnection unwrapped = ( ( Wrapper<LdapConnection> ) connection ).wrapped();
            return verify( unwrapped, mode );
        }
    }
    throw new NotAMockException( "connection is not a mock, nor a wrapper for a connection that is one" );
}
 
Example 2
Source File: StubberImpl.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public <T> T when(T mock) {
    MockUtil mockUtil = new MockUtil();
    
    if (mock == null) {
        reporter.nullPassedToWhenMethod();
    } else {
        if (!mockUtil.isMock(mock)) {
            reporter.notAMockPassedToWhenMethod();
        }
    }
    
    mockUtil.getMockHandler(mock).setAnswersForStubbing(answers);
    return mock;
}
 
Example 3
Source File: StubberImpl.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public <T> T when(T mock) {
    MockUtil mockUtil = new MockUtil();
    
    if (mock == null) {
        reporter.nullPassedToWhenMethod();
    } else {
        if (!mockUtil.isMock(mock)) {
            reporter.notAMockPassedToWhenMethod();
        }
    }
    
    mockUtil.getMockHandler(mock).setAnswersForStubbing(answers);
    return mock;
}
 
Example 4
Source File: ToStringCheckerTest.java    From storio with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldCreateSampleValueOfAnyObject() {
    Object sample = new ToStringChecker<Object>(Object.class)
            .createSampleValueOfType(Object.class);

    MockUtil.isMock(sample);
    assertThat(sample.getClass().getSuperclass()).isEqualTo(Object.class);
}
 
Example 5
Source File: MethodForwardingTestUtil.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * This is a best effort automatic test for method forwarding between a delegate and its wrapper, where the wrapper
 * class is a subtype of the delegate. Methods can be remapped in case that the implementation does not call the
 * original method. Remapping to null skips the method. This ignores methods that are inherited from Object.
 *
 * @param delegateClass the class for the delegate.
 * @param wrapperFactory factory that produces a wrapper from a delegate.
 * @param delegateObjectSupplier supplier for the delegate object passed to the wrapper factory.
 * @param skipMethodSet set of methods to ignore.
 * @param <D> type of the delegate
 * @param <W> type of the wrapper
 * @param <I> type of the object created as delegate, is a subtype of D.
 */
public static <D, W, I extends D> void testMethodForwarding(
	Class<D> delegateClass,
	Function<I, W> wrapperFactory,
	Supplier<I> delegateObjectSupplier,
	Set<Method> skipMethodSet) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {

	Preconditions.checkNotNull(delegateClass);
	Preconditions.checkNotNull(wrapperFactory);
	Preconditions.checkNotNull(skipMethodSet);

	I delegate = delegateObjectSupplier.get();

	//check if we need to wrap the delegate object as a spy, or if it is already testable with Mockito.
	if (!MockUtil.isSpy(delegate) || !MockUtil.isMock(delegate)) {
		delegate = spy(delegate);
	}

	W wrapper = wrapperFactory.apply(delegate);

	// ensure that wrapper is a subtype of delegate
	Preconditions.checkArgument(delegateClass.isAssignableFrom(wrapper.getClass()));

	for (Method delegateMethod : delegateClass.getMethods()) {

		if (checkSkipMethodForwardCheck(delegateMethod, skipMethodSet)) {
			continue;
		}

		// find the correct method to substitute the bridge for erased generic types.
		// if this doesn't work, the user need to exclude the method and write an additional test.
		Method wrapperMethod = wrapper.getClass().getMethod(
			delegateMethod.getName(),
			delegateMethod.getParameterTypes());

		// things get a bit fuzzy here, best effort to find a match but this might end up with a wrong method.
		if (wrapperMethod.isBridge()) {
			for (Method method : wrapper.getClass().getMethods()) {
				if (!method.isBridge()
					&& method.getName().equals(wrapperMethod.getName())
					&& method.getParameterCount() == wrapperMethod.getParameterCount()) {
					wrapperMethod = method;
					break;
				}
			}
		}

		Class<?>[] parameterTypes = wrapperMethod.getParameterTypes();
		Object[] arguments = new Object[parameterTypes.length];
		for (int j = 0; j < arguments.length; j++) {
			Class<?> parameterType = parameterTypes[j];
			if (parameterType.isArray()) {
				arguments[j] = Array.newInstance(parameterType.getComponentType(), 0);
			} else if (parameterType.isPrimitive()) {
				if (boolean.class.equals(parameterType)) {
					arguments[j] = false;
				} else if (char.class.equals(parameterType)) {
					arguments[j] = 'a';
				} else {
					arguments[j] = (byte) 0;
				}
			} else {
				arguments[j] = Mockito.mock(parameterType);
			}
		}

		wrapperMethod.invoke(wrapper, arguments);
		delegateMethod.invoke(Mockito.verify(delegate, Mockito.times(1)), arguments);
		reset(delegate);
	}
}
 
Example 6
Source File: MethodForwardingTestUtil.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * This is a best effort automatic test for method forwarding between a delegate and its wrapper, where the wrapper
 * class is a subtype of the delegate. Methods can be remapped in case that the implementation does not call the
 * original method. Remapping to null skips the method. This ignores methods that are inherited from Object.
 *
 * @param delegateClass the class for the delegate.
 * @param wrapperFactory factory that produces a wrapper from a delegate.
 * @param delegateObjectSupplier supplier for the delegate object passed to the wrapper factory.
 * @param skipMethodSet set of methods to ignore.
 * @param <D> type of the delegate
 * @param <W> type of the wrapper
 * @param <I> type of the object created as delegate, is a subtype of D.
 */
public static <D, W, I extends D> void testMethodForwarding(
	Class<D> delegateClass,
	Function<I, W> wrapperFactory,
	Supplier<I> delegateObjectSupplier,
	Set<Method> skipMethodSet) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {

	Preconditions.checkNotNull(delegateClass);
	Preconditions.checkNotNull(wrapperFactory);
	Preconditions.checkNotNull(skipMethodSet);

	I delegate = delegateObjectSupplier.get();

	//check if we need to wrap the delegate object as a spy, or if it is already testable with Mockito.
	if (!MockUtil.isSpy(delegate) || !MockUtil.isMock(delegate)) {
		delegate = spy(delegate);
	}

	W wrapper = wrapperFactory.apply(delegate);

	// ensure that wrapper is a subtype of delegate
	Preconditions.checkArgument(delegateClass.isAssignableFrom(wrapper.getClass()));

	for (Method delegateMethod : delegateClass.getMethods()) {

		if (checkSkipMethodForwardCheck(delegateMethod, skipMethodSet)) {
			continue;
		}

		// find the correct method to substitute the bridge for erased generic types.
		// if this doesn't work, the user need to exclude the method and write an additional test.
		Method wrapperMethod = wrapper.getClass().getMethod(
			delegateMethod.getName(),
			delegateMethod.getParameterTypes());

		// things get a bit fuzzy here, best effort to find a match but this might end up with a wrong method.
		if (wrapperMethod.isBridge()) {
			for (Method method : wrapper.getClass().getMethods()) {
				if (!method.isBridge()
					&& method.getName().equals(wrapperMethod.getName())
					&& method.getParameterCount() == wrapperMethod.getParameterCount()) {
					wrapperMethod = method;
					break;
				}
			}
		}

		Class<?>[] parameterTypes = wrapperMethod.getParameterTypes();
		Object[] arguments = new Object[parameterTypes.length];
		for (int j = 0; j < arguments.length; j++) {
			Class<?> parameterType = parameterTypes[j];
			if (parameterType.isArray()) {
				arguments[j] = Array.newInstance(parameterType.getComponentType(), 0);
			} else if (parameterType.isPrimitive()) {
				if (boolean.class.equals(parameterType)) {
					arguments[j] = false;
				} else if (char.class.equals(parameterType)) {
					arguments[j] = 'a';
				} else {
					arguments[j] = (byte) 0;
				}
			} else {
				arguments[j] = Mockito.mock(parameterType);
			}
		}

		wrapperMethod.invoke(wrapper, arguments);
		delegateMethod.invoke(Mockito.verify(delegate, Mockito.times(1)), arguments);
		reset(delegate);
	}
}
 
Example 7
Source File: HashCodeAndEqualsMockWrapper.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Override public String toString() {
    MockUtil mockUtil = new MockUtil();
    return "HashCodeAndEqualsMockWrapper{" +
            "mockInstance=" + (mockUtil.isMock(mockInstance) ? mockUtil.getMockName(mockInstance) : typeInstanceString()) +
            '}';
}
 
Example 8
Source File: MethodForwardingTestUtil.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * This is a best effort automatic test for method forwarding between a delegate and its wrapper, where the wrapper
 * class is a subtype of the delegate. Methods can be remapped in case that the implementation does not call the
 * original method. Remapping to null skips the method. This ignores methods that are inherited from Object.
 *
 * @param delegateClass the class for the delegate.
 * @param wrapperFactory factory that produces a wrapper from a delegate.
 * @param delegateObjectSupplier supplier for the delegate object passed to the wrapper factory.
 * @param skipMethodSet set of methods to ignore.
 * @param <D> type of the delegate
 * @param <W> type of the wrapper
 * @param <I> type of the object created as delegate, is a subtype of D.
 */
public static <D, W, I extends D> void testMethodForwarding(
	Class<D> delegateClass,
	Function<I, W> wrapperFactory,
	Supplier<I> delegateObjectSupplier,
	Set<Method> skipMethodSet) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {

	Preconditions.checkNotNull(delegateClass);
	Preconditions.checkNotNull(wrapperFactory);
	Preconditions.checkNotNull(skipMethodSet);

	I delegate = delegateObjectSupplier.get();

	//check if we need to wrap the delegate object as a spy, or if it is already testable with Mockito.
	if (!MockUtil.isSpy(delegate) || !MockUtil.isMock(delegate)) {
		delegate = spy(delegate);
	}

	W wrapper = wrapperFactory.apply(delegate);

	// ensure that wrapper is a subtype of delegate
	Preconditions.checkArgument(delegateClass.isAssignableFrom(wrapper.getClass()));

	for (Method delegateMethod : delegateClass.getMethods()) {

		if (checkSkipMethodForwardCheck(delegateMethod, skipMethodSet)) {
			continue;
		}

		// find the correct method to substitute the bridge for erased generic types.
		// if this doesn't work, the user need to exclude the method and write an additional test.
		Method wrapperMethod = wrapper.getClass().getMethod(
			delegateMethod.getName(),
			delegateMethod.getParameterTypes());

		// things get a bit fuzzy here, best effort to find a match but this might end up with a wrong method.
		if (wrapperMethod.isBridge()) {
			for (Method method : wrapper.getClass().getMethods()) {
				if (!method.isBridge()
					&& method.getName().equals(wrapperMethod.getName())
					&& method.getParameterCount() == wrapperMethod.getParameterCount()) {
					wrapperMethod = method;
					break;
				}
			}
		}

		Class<?>[] parameterTypes = wrapperMethod.getParameterTypes();
		Object[] arguments = new Object[parameterTypes.length];
		for (int j = 0; j < arguments.length; j++) {
			Class<?> parameterType = parameterTypes[j];
			if (parameterType.isArray()) {
				arguments[j] = Array.newInstance(parameterType.getComponentType(), 0);
			} else if (parameterType.isPrimitive()) {
				if (boolean.class.equals(parameterType)) {
					arguments[j] = false;
				} else if (char.class.equals(parameterType)) {
					arguments[j] = 'a';
				} else {
					arguments[j] = (byte) 0;
				}
			} else {
				arguments[j] = Mockito.mock(parameterType);
			}
		}

		wrapperMethod.invoke(wrapper, arguments);
		delegateMethod.invoke(Mockito.verify(delegate, Mockito.times(1)), arguments);
		reset(delegate);
	}
}