You are creating Spring bean using new
operator:
MyClass obj = new MyClass();
this almost never works1. You need to ask Spring container to provide fully operational, initialized instance of MyClass
bean, e.g. with autowiring/DI inside your test case:
@ContextConfiguration( locations={..path to xmls..} )
class IntegTestBase{
@Autowired
protected MyClass obj;
}
Later in your test simply use it:
@Test
public testingMethod(){
//don't recreate `obj` here!
obj.methodToBeTested();
---- some assertions ----
}
In fact this is the functionality that Spring Test Support provides. You must also remember that obj
will point to the exact same instance in every test if MyClass
is a singleton, but that's typically not a problem.
1 theoretically possible with full AspectJ, but you don't want to go that path.