如何测试使用JUnit测试HQL查询?测试HQL查询上使用JUnit
我已经映射实体类:
@Entity
@Table(name = "DOE")
public DomainObjectEntity {
//some attributes
}
代表域对象:
public class DomainObject {
//some attributes
}
我也有仓库接口为我的域对象:
public interface DomainObjectRepository {
/**
* Gets entity by some attribute
*
*/
DomainObject getDomainObjectByObjectId(String objectId);
}
这个接口实现在
@Repository
public class DomainObjectRepositoryImpl implements DomainObjectRepository {
@Inject
private DomainObjectEntityJPARepository entityRepository;
@Override
public DomainObjectgetDomainObjectById(String parcelId) {
//this converts my entity to domain object
return entityRepository.findByDomainObjectId(DomainObjectId.getStr()).getDomainObject();
}
}
我JPA实体库看起来是这样的:
public interface DomainObjectEntityJPARepository extends JpaRepository<DomainObjectEntity, String> {
/**
* get DomainObject with requested id
*
* @param objectId - objects id
* @return DomainObject
*/
@Query("some query to be tested")
DomainObjectEntity findByDomainObjectId(@Param("objectId") String objectId);
}
,最后我想要写一个简单的JUnit测试,以检查是否查询findByDomainObjectId
返回正确的对象。我觉得我的测试应该是这样的:
- 创建测试对象投入DB
- 从数据库中检索对象
- 比较对象
我已经设置好的了import.sql一个hbm2dll创建和填充我的数据库,但我如何通过调用方法findByDomainObjectId
从Junit测试访问它?
谢谢,这实际上帮了我很多!但是我决定做单元测试,因为我只想测试querry,会提供解决方案,在第二个工作 –