Changing method execution in runtime (Java)
<p>I am a Software Engineer in Test, and I am trying to write code that can replace production side method so that test can execute those instead. Basically, I do not want to modify production code for testability.</p>

<p>Here is a simple scenario:</p>

<pre><code>public class Foo {

     public static void foo() {
         printA();
     }

     public static void printA() {
         System.out.println("A");
     }

     public static void printB() {
         System.out.println("B");
     }
}

public class Foobar {

    public Foobar() {

    }

    public void test() {
        Foo.foo();
    }

    public static void main(String[] args) {
        //Try changing the method here

        new Foobar().test();
    }

}
</code></pre>

<p>As you can see, when the main executes, it will print "A" since it calls the method printA on static method foo(). Now on runtime, is there a way I can inject or modify such that foo will call printB instead of printA?</p>

<p>Thank you for all the help!</p>
