Пример #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();
    }
  }
Пример #2
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);
 }
Пример #3
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();
 }