Java 如何使用@Autowired字段测试泛型抽象类?
我有一个泛型抽象类AbstractBaseEntityGenericDao,它包含@Autowired字段。它工作得很好,直到我不得不为它编写一个单元测试,在所有扩展它的类的测试中不重复相同的代码。现在我在想…有可能为这样的类编写单元/集成测试吗Java 如何使用@Autowired字段测试泛型抽象类?,java,spring,junit,mocking,Java,Spring,Junit,Mocking,我有一个泛型抽象类AbstractBaseEntityGenericDao,它包含@Autowired字段。它工作得很好,直到我不得不为它编写一个单元测试,在所有扩展它的类的测试中不重复相同的代码。现在我在想…有可能为这样的类编写单元/集成测试吗 @Repository @Transactional public abstract class AbstractBaseEntityGenericDao<T extends BaseEntity> { private Class
@Repository
@Transactional
public abstract class AbstractBaseEntityGenericDao<T extends BaseEntity> {
private Class<T> classInstance;
private SessionFactory sessionFactory;
@Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public final void setClassInstance(Class<T> clasInstance) {
this.classInstance = clasInstance;
}
public void create(@NonNull T entity) {
Session session = sessionFactory.getCurrentSession();
session.save(entity);
}
public Optional<T> find(long id) {
Session session = sessionFactory.getCurrentSession();
return Optional.ofNullable(session.get(classInstance, id));
}
public void update(@NonNull T entity) {
Session session = sessionFactory.getCurrentSession();
session.saveOrUpdate(entity);
}
public void remove(@NonNull Long id) throws EntityNotFoundException {
Session session = sessionFactory.getCurrentSession();
session.remove(session.load(classInstance, id));
}
public void remove(@NonNull T entity) {
Session session = sessionFactory.getCurrentSession();
session.remove(entity);
}
}
@存储库
@交易的
公共抽象类AbstractBaseEntityGenericDao{
私家阶级;
私人会话工厂会话工厂;
@自动连线
public void setSessionFactory(SessionFactory SessionFactory){
this.sessionFactory=sessionFactory;
}
公共最终无效集合类别实例(类别实例){
this.classInstance=clasInstance;
}
公共void create(@NonNull T entity){
Session Session=sessionFactory.getCurrentSession();
session.save(实体);
}
公共可选查找(长id){
Session Session=sessionFactory.getCurrentSession();
返回可选的.ofNullable(session.get(classInstance,id));
}
公共无效更新(@NonNull T entity){
Session Session=sessionFactory.getCurrentSession();
会话。保存或更新(实体);
}
public void remove(@NonNull Long id)引发EntityNotFoundException{
Session Session=sessionFactory.getCurrentSession();
session.remove(session.load(classInstance,id));
}
公共无效删除(@NonNull T entity){
Session Session=sessionFactory.getCurrentSession();
删除(实体);
}
}
这之所以困难,是因为通常情况下您不应该这样做。抽象类不应该知道其子类如何创建SessionFactory
。因此,它应该看起来像:
@Repository
@Transactional
public abstract class AbstractBaseEntityGenericDao<T extends BaseEntity> {
...
protected SessionFactory sessionFactory;
...
}
@Autowired注释描述了SessionFactory对象的依赖项注入。您编写单元测试的具体目的是什么?你在测试AbstractBaseEntityGenericDao中的方法吗?是的。也许这更像是一个集成测试,但无论如何……这个解决方案非常适合。非常感谢。
public class AbstractBaseEntityGenericDaoTest {
private class AbstractClassStub extends AbstractBaseEntityGenericDao {
public AbstractClassStub(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public void create(BaseEntity entity) {
super.create(entity);
}
@Override
public Optional find(long id) {
return super.find(id);
}
@Override
public void update(BaseEntity entity) {
super.update(entity);
}
@Override
public void remove(@NonNull Long id) throws EntityNotFoundException {
super.remove(id);
}
@Override
public void remove(BaseEntity entity) {
super.remove(entity);
}
}
@Mock
SessionFactory sessionFactory;
private AbstractClassStub abstractClassStub;
@Before
public void before() {
sessionFactory = mock(SessionFactory.class);
abstractClassStub = new AbstractClassStub(sessionFactory);
}
@Test
public void testWhatever() {
abstractClassStub.find(1); //or whatever
}
}