vendredi 29 mai 2015

jUnit change behavior of a method

I have to make some jUnit tests for some methods and I can't change the source code. Is there any possibility to change the behavior of a function without change source code? Look a straight-forward example: Class A and B are source code (can't change them). I want to change behavior of run() method from A when I call it in B through testing() in Junit test. Any ideas?

public class A {
    public String run(){
        return "test";
    } 
}

public class B {
    public void testing() {
        String fromA = new A().run(); //I want a mocked result here
        System.out.println(fromA);
    }
}

public class C {
    @Test
    public void jUnitTest() {
        new B().testing();
        // And here i want to call testing method from B but with a "mock return" from run()         
    }
}

Aucun commentaire:

Enregistrer un commentaire