I am interested in Mocking objects in a JUnit test-suite, however I have only come across mocking frameworks that use dependency injection to inject mock objects. However, I would like to be able to mock classes/functions without having to inject that mock object, along the lines of @patch() in python.
Trivial example:
//dependency injection
public String bar(Foo foo) {
return foo.foo(); //just pass in mock Foo object
}
//.... onto test code
Foo mockedFoo = <Mocked Foo object>;
String response = bar(mockedFoo);
assertEqual(response, <mockedFoo return value>);
//case I am looking for
public String bar() {
Foo foo = new Foo(); //how to create mock object here?
return foo.foo(); //or simply how to mock a single function?
}
//... onto test code
<force the Foo class or foo method to be mocked from here without touching bar() source code>
String response = bar();
assertEqual(response, <mocked response>);