我正在尝试在Spring Boot中编写DAO层的基本测试。我正在使用文档中所述的所有内容,但是我不断收到bean错误:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.vancouverproject.application.MealDaoIntegrationTest': Unsatisfied dependency expressed through field 'mealDao'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.vancouverproject.application.dao.MealDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
我的测试看起来很简单:
@RunWith(SpringRunner.class)
@DataJpaTest
public class MealDaoIntegrationTest {
@Autowired
private TestEntityManager entityManager;
@Autowired
private MealDao mealDao;
@Test
public void findByMealTest() {
// given
Meal meal1 = new Meal();
meal1.setId(1);
entityManager.persist(meal1);
entityManager.flush();
// when
Optional<Meal> found1 = mealDao.findById(meal1.getId());
// then
Assert.assertEquals(Optional.of(found1.get().getId()), meal1.getId());
}
我已经检查过将它们全部包装在同一个包装中Application.class +我已经确保我的Dao和DaoImpl类两者都有@Repository批注(以便DataJpaTest可以看到它)。我的界面:
@Repository
public interface MealDao extends IAbstractDao<Meal>{
List<Meal> findMealByType(MealType mealType); }
DaoImpl:
@Repository
public class MealDaoImpl extends AbstractDao<Meal> implements MealDao {
@Override
protected Class<Meal> getEntityClass() {
return Meal.class;
}
@Override
public List<Meal> findMealByType(MealType mealType) {
return em.createNamedQuery(Meal.QUERY_FIND_BY_MEAL_TYPE, Meal.class).setParameter("mealType", mealType).getResultList();
}
@Repository
public abstract class AbstractDao<T> implements IAbstractDao<T> {
@PersistenceContext
protected EntityManager em;
protected abstract Class<T> getEntityClass();
public Optional<T> findById(long id) {
return Optional.ofNullable(em.find(getEntityClass(), id));
}
如果您需要代码的其他部分,请告诉我。仍然不知道该问题从何而来。
更新:
@Repository
public interface IAbstractDao<T> {
Optional<T> findById(long id);
List<T> findAll();
T merge(T object);
}
Spring无法实例化MealDAO所需的Bean。
接口IAbstractDao缺少扩展名,它不应是存储库,例如:
@NoRepositoryBean
public interface IAbstractDao<T extends Meal>
extends CrudRepository<T, Long> {
public T findAll();
}
然后您可以通过以下方式使用此界面:
@Transactional
public interface BananaRepository extends IAbstractDao<Banana> { /* ... */ }
香蕉属于膳食类型,而膳食则是抽象类。然后,您还无需检查餐食类型,因为这可以减少代码的复杂性并使其更易于理解。
您应该先看看:https://blog.netgloo.com/2014/12/18/handling-entities-inheritance-with-spring-data-jpa/
总结起来很不错,如何使用Spring解决存储库(DAO)中的此类f继承。
Spring无法实例化MealDAO所需的Bean。
接口IAbstractDao缺少扩展名,它不应是存储库,例如:
@NoRepositoryBean
public interface IAbstractDao<T extends Meal>
extends CrudRepository<T, Long> {
public T findAll();
}
然后您可以通过以下方式使用此界面:
@Transactional
public interface BananaRepository extends IAbstractDao<Banana> { /* ... */ }
香蕉属于膳食类型,而膳食则是抽象类。然后,您还无需检查餐食类型,因为这可以减少代码的复杂性并使其更易于理解。
您应该先看看:https://blog.netgloo.com/2014/12/18/handling-entities-inheritance-with-spring-data-jpa/
总结起来很不错,如何使用Spring解决存储库(DAO)中的此类f继承。