Extracting a new method

In this section, you will improve the code of the constructor of junit.framework.TestSuite. To make the intent of the code clearer, you will extract the code that collects test cases from base classes into a new method called collectInheritedTests.

  1. In the junit.framework/TestSuite.java file, select the following range of code inside the TestSuite(Class) constructor:

    Class superClass= theClass;
    Vector names= new Vector();
    while(Test.class.isAssignableFrom(superClass)) {
        Method[] methods= superClass.getDeclaredMethods();
        for (int i= 0; i < methods.length; i++) {
            addTestMethod(methods[i],names, theClass);
        }
        superClass= superClass.getSuperclass();
    }

  2. From the selection's context menu in the editor, select Refactor > Extract Method....

    Context menu extract method

  3. In the Method Name field, type collectInheritedTests.

    Extract method wizard data input page

  4. To preview the changes, press Preview>.The preview page displays the changes that will be made. Press OK to extract the method.

    Extract method preview page

  5. Go to the extracted method by selecting it in the Outline view.

    Extracted method

Related concepts

Java editor
Refactoring support

Related reference

Java Preferences