I have a Class that has a field that gets set through a spring bean and is annotated with @Autowired like so:
public class Foo {
....
@Autowired
private Bar bar;
....
}
I now want to setup a JUnit test that tests some functionality that at some point will create a Foo object. I am not in control of when this Foo object is generated. What is the easiest way to mock this object so that anytime Foo gets instantiated, this object get populated with the mocked item? I am using a 3rd party library to mock the Bar object nicely, so I would prefer to do all of this in the Java test class without using xml files.
Everything I have found online seems to either involve creating an xml file or mocking a specific instance of the Class. Thank you all for your help!
I would like something like the following:
public class Test {
private Bar mockedBar = createBar();
// somehow set 'mockedBar' so that any Foo object
// created always contains mockedBar
}