如何測試使用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,會提供解決方案,在第二個工作 –