Example #1
0
  public void registerMockedClass(@Nonnull Class<?> mockedType) {
    if (!isMockedClass(mockedType)) {
      if (Proxy.isProxyClass(mockedType)) {
        mockedType = mockedType.getInterfaces()[0];
      }

      mockedClasses.add(mockedType);
    }
  }
  /**
   * Creates a {@link Proxy} implementation for a given interface, in which all methods are empty.
   * Non-void methods will return a default value according to the return type: {@literal 0} for
   * {@code int}, {@literal null} for a reference type, and so on.
   *
   * <p>The {@code equals}, {@code hashCode}, and {@code toString} methods inherited from {@code
   * java.lang.Object} are overridden with an appropriate implementation in each case: {@code
   * equals} is implemented by comparing the two object references (the proxy instance and the
   * method argument) for equality; {@code hashCode} is implemented to return the identity hash code
   * for the proxy instance; and {@code toString} returns the standard string representation that
   * {@code Object#toString} would have returned.
   *
   * <p>This method is just a convenience for some uses of the <em>Mockups</em> API. In <em>JMockit
   * Expectations</em> in particular, mocked instances will be automatically created and assigned to
   * any mock fields or parameters.
   *
   * @param loader the class loader under which to define the proxy class; usually this would be the
   *     application class loader, which can be obtained from any application class
   * @param interfaceToBeProxied a {@code Class} object for an interface
   * @return the created proxy instance
   * @see #newEmptyProxy(Class)
   * @see #newEmptyProxy(Type...)
   * @see <a
   *     href="http://code.google.com/p/jmockit/source/browse/trunk/samples/orderMngmntWebapp/test/orderMngr/domain/order/OrderRepository_MockupsAPI_Test.java#186">
   *     Example</a>
   */
  public static <E> E newEmptyProxy(ClassLoader loader, Class<E> interfaceToBeProxied) {
    Class<?>[] interfaces =
        loader == null
            ? new Class<?>[] {interfaceToBeProxied}
            : new Class<?>[] {interfaceToBeProxied, EmptyProxy.class};

    //noinspection unchecked
    return (E) Proxy.newProxyInstance(loader, interfaces, MockInvocationHandler.INSTANCE);
  }