我正在学习 Mockito。我在为嵌套对象创建模拟时遇到问题。看
public interface BaseManager {
public Query createQuery(String queryString);
}
和一个实现类
public class BaseManagerImpl implements BaseManager {
@Autowired
private SessionFactory sessionFactory;
// ...
}
模块级休眠管理器,例如:
public interface RegistrationManager {
@Transactional
public List<Country> getCountries();
}
和一个实现类
public class RegistrationManagerImpl implements RegistrationManager {
@Autowired
private BaseManager baseManager;
// ...
}
现在我在创建模拟基础管理器时遇到了问题。我的测试课是:
public class MockitoTest {
private RegistrationManager registrationManager = new RegistrationManagerImpl();
@Mock private BaseManager baseManager;
@Mock private SessionFactory sessionFactory;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
// baseManager.setSessionFactory(sessionFactory);
registrationManager.setBaseManager(baseManager);
}
// ...
// @Test
}
问题:sessionFactory 没有在 baseManager 中实例化。 请帮助创建模拟对象。提前致谢!