Exemplo n.º 1
0
  /**
   * Sets up the <em>startup</em> {@linkplain Mock mocks} defined in one or more mock classes, just
   * like {@link #setUpMocks(Object...)} does for regular mock classes. The difference is in the
   * lifetime of the mocks, which will last to the end of the test run. Consequently, this method
   * should only be called once, before the first test begins execution. One way to achieve this is
   * to put the call in the static initializer of a common base class extended by all test classes
   * in the suite. Another way is by configuring what happens at startup through external means.
   *
   * <p>There are three ways to set up mock classes at startup time:
   *
   * <ol>
   *   <li>Define a value for the "<code>jmockit-mocks</code>" system property, as a comma-separated
   *       list of fully qualified class names.
   *   <li>Add a custom "<code>jmockit.properties</code>" file to the classpath, with an entry for
   *       the "<code>jmockit-mocks</code>" (or just "<code>mocks</code>") property.
   *   <li>Specify the "<code>-javaagent:jmockit.jar=&lt;agentArgs></code>" JVM argument, with "
   *       <code>agentArgs</code>" containing one or more mock class names, separated by semicolons
   *       if more than one.
   * </ol>
   *
   * Note that option two above makes it possible to package a whole set of reusable mock classes in
   * a jar file, provided it contains a suitable <code>jmockit.properties</code> file. By simply
   * adding the jar to the classpath <em>before</em> <code>jmockit.jar</code>, the specified mock
   * classes will be loaded and applied automatically on every test run, as soon as JMockit itself
   * gets initialized.
   *
   * @param mockClassesOrInstances one or more mock classes (either <code>Class</code> literals or
   *     fully qualified class names) or instances of mock classes
   * @throws IllegalArgumentException if a given mock class fails to specify the corresponding real
   *     class using the {@code @MockClass(realClass = ...)} annotation; or if a mock class defines
   *     a mock method for which no corresponding real method or constructor exists in the real
   *     class; or if the real method matching a mock method is {@code abstract}
   * @see <a
   *     href="http://code.google.com/p/jmockit/source/browse/trunk/main/test/mockit/MockAnnotationsTest.java#465">
   *     Example</a>
   *     <p>In the Tutorial: <a
   *     href="http://jmockit.googlecode.com/svn/trunk/www/tutorial/UsingMocksAndStubs.html">Using
   *     mocks and stubs over entire test classes and suites</a>
   */
  public static void setUpStartupMocks(Object... mockClassesOrInstances) {
    for (Object mockClassOrInstance : mockClassesOrInstances) {
      Class<?> mockClass;
      Object mock;

      if (mockClassOrInstance instanceof Class<?>) {
        mockClass = (Class<?>) mockClassOrInstance;
        mock = null;
      } else if (mockClassOrInstance instanceof String) {
        String className = ((String) mockClassOrInstance).trim();
        if (className.length() == 0) continue;
        mockClass = Utilities.loadClass(className);
        mock = null;
      } else {
        mockClass = mockClassOrInstance.getClass();
        mock = mockClassOrInstance;
      }

      new MockClassSetup(mock, mockClass).setUpStartupMock();
    }
  }
Exemplo n.º 2
0
 /**
  * Creates a {@link Proxy} implementation for a given set of interface types. In this created
  * class all methods will be empty, with return values for non-void methods being the appropriate
  * default value ({@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 interfacesToBeProxied one or more {@code Type} objects, each of which can be a {@code
  *     Class} object for an interface, a {@link ParameterizedType} whose raw type is an interface,
  *     or a {@link TypeVariable} whose bounds are interfaces
  * @return the created proxy instance
  */
 public static <E> E newEmptyProxy(Type... interfacesToBeProxied) {
   return Utilities.newEmptyProxy(null, interfacesToBeProxied);
 }
Exemplo n.º 3
0
 /**
  * Same as {@link #setUpMock(Class, Class)}, but accepting the (fully qualified) name of the real
  * class. This is useful when said class is not accessible from the test.
  *
  * @see <a
  *     href="http://code.google.com/p/jmockit/source/browse/trunk/main/test/integrationTests/textFile/TextFileUsingAnnotatedMockClassesTest.java#40">
  *     Example</a>
  */
 public static void setUpMock(String realClassName, Class<?> mockClass) {
   Class<?> realClass = Utilities.loadClass(realClassName);
   setUpMock(realClass, mockClass);
 }
Exemplo n.º 4
0
 /**
  * Same as {@link #stubOutClass(Class, boolean, String...)}, but accepting the (fully qualified)
  * name of the real class. This is useful when said class is not accessible from the test.
  */
 public static void stubOutClass(String realClassName, boolean inverse, String... filters) {
   Class<?> realClass = Utilities.loadClass(realClassName);
   new ClassStubbing(realClass, !inverse, filters).stubOut();
 }