Embed a test project in legacy code

Given: A LegacyControllerClass, which expands MonsterFrameworkClass(part of a very vibrant structure with which people simply live for years). The Framework class has a lot of magic: from tons of logic in the default constructor to static blocks that reflectively load classes.

Many life cycle techniques LegacyControllerClassthat mutate global states. The method execute()is a thousand liners that have all the evil you can think of.

public class LegacyControllerClass extends MonsterFrameworkClass {

   //Life-cycle method
   public void validate(){...}

   //Life-cycle method
   public void preExecute() {...}

   //Life-cycle method
   public void execute() {
       //oodles of legacy code here:
       //    examples: call static methods of 'Util' classes
       //              lots of new X().y(..)
       //              call databases, services
       //              Even call super.execute()!!
       //              More rubbish
       //              More rubbish 
   }
}

, execute(). Test-Driven-Development , , "". "" responseProperties, view (jsp) . :

if (error) {
    Build the error message
    Add the error message into the responseProperties
}

, , execute().

: ? , , :

  • rubbish1() rubbish2()
  • (, - )
  • rubbish1() rubbish2()

, MonsterFrameworkClass : , , ResourceBundle ..

?

Teeny-tiny . " " gulp , SO, , .

+4
1

. Powermock, . , , .

, - . MonstrosityRubbish ( - ), .

- - , , .

, :

private MyShinyClass yourShinyObject; // init

public void execute(Param param0) {
    rubbish1();
    yourShinyObject.handleError(param0);
    rubbish2();
}

public class MyShinyClass {

    public void handleError(Param param0) {
        // your code here
    }
}

, Param, /, .

, , responseProperties :

public void execute(Param param0) {
    rubbish1();
    responseProperties.add(yourShinyObject.fetchErrors(param0));
    rubbish2();
}

public class MyShinyClass {

    public List<HopelessError> fetchErrors(Param param0) {
        // your code here
    }
}

, execute() , /, handleErrors().

+1

All Articles