The more I read the layout, the more I get confused ...
I have a classA eat () method that calls FatDude eatThemAll () class
public class classA { FatDude dude = new FatDude(); public String eat() { String result = dude.eatThemAll(); } } public class FatDude { public String eatThemAll() { return "never full"; } }
Now I want to test the classA eat () method, mocking the FatDude class.
public class MockFatDude extends FatDude {
This DoTest runTest () will not use the MockFatDude class. One way I can think of is to change the code to pass the FatDude method to the eat () method of class A:
public class classA { public String eat(FatDude dude) { String result = dude.eatThemAll(); } }
Then change my testing method to:
public class DoTest { public void runTest() { classA cl = new ClassA(); String out = cl.eat(new MockFatDude()); assertEqual(out, "very full"); } }
But, as you can see, I had to change the source code to satisfy my need. Is it correct? What if I am not allowed to modify the source code? I know if I am applying the TDD concept, it is fine to change the source code, but I would like to hear some opinion or advice if what I showed above is the right way.
source share