![](/img/trans.png)
[英]Spring Boot + Hibernate Web Application: How to globally intercept native and managed entity queries?
[英]How to test native queries in Spring Boot application?
通過遵循TDD方法,我在編碼方面是全新的。 現在,我不確定每個代碼在編寫之前是否都需要測試用例。 如何使用if-else條件測試本機查詢?
我已經研究了以下一些方法,但並不能滿足我的問題:
https://stackoverflow.com/search?q=test+entitymanager
https://stackoverflow.com/search?q=entitymanager+returned+null+in+testing
@Transactional
public class ProfileRepositoryCustomImpl implements ProfileRepositoryCustom {
@PersistenceContext
private EntityManager entityManager;
@Override
public List<ProfileMinimalResponseDTO> searchProfile(ProfileDTO profileDTO) {
Query query = entityManager.createNativeQuery(QueryCreator.createQueryToSearchProfile.apply(profileDTO));
List<Object[]> list = query.getResultList();
if (ObjectUtils.isEmpty(list))
throw new NoContentFoundException(NoRecordsFound.MESSAGE, NoRecordsFound.DEVELOPER_MESSAGE);
return list.stream().map(
ProfileUtils.convertObjectToProfileResponseDTO)
.collect(Collectors.toList());
}
}
實體看起來像:
@Entity
@Table(name = "profile")
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Data
public class Profile implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
private String name;
@Column(name = "description")
private String description;
@Column(name = "status")
private Character status;
@Column(name = "department_id")
private Long departmentId;
@Column(name = "sub_department_id")
private Long subDepartmentId;
}
public class QueryCreator {
public static Function<ProfileDTO, String> createQueryToSearchProfile = (profileDTO -> {
String query = "";
query += " SELECT" +
" p.id," + //[0]
" p.name," + //[1]
" p.status," + //[2]
" p.department_id," + //[3]
" p.sub_department_id" + //[4]
" FROM" +
" profile p" +
" WHERE p.id!=0";
if (!Objects.isNull(profileDTO)) {
if (!Objects.isNull(profileDTO.getName()))
query += " AND p.name='" + profileDTO.getName() + "'";
if (!Objects.isNull(profileDTO.getDepartmentId()))
query += " AND p.department_id=" + profileDTO.getDepartmentId();
if (!Objects.isNull(profileDTO.getSubDepartmentId()))
query += " AND p.sub_department_id=" + profileDTO.getDepartmentId();
}
return query;
});
}
public static Function<Object[], ProfileMinimalResponseDTO> convertObjectToProfileResponseDTO = (objects) -> {
final Integer ID = 0;
final Integer NAME = 1;
final Integer STATUS = 2;
final Integer DEPARTMENT_ID = 3;
final Integer SUB_DEPARTMENT_ID = 4;
return ProfileMinimalResponseDTO.builder()
.id(Long.parseLong(objects[ID].toString()))
.name(objects[NAME].toString())
.status(objects[STATUS].toString().charAt(0))
.departmentId(Long.parseLong(objects[DEPARTMENT_ID].toString()))
.subDepartmentId(Long.parseLong(objects[SUB_DEPARTMENT_ID].toString()))
.build();
};
我期待有關如何為上述場景編寫測試用例的答案。 此外,任何有關學習TDD的參考都將非常友善和有益。
關於學習TDD的參考資料,我肯定會推薦一本經典的書,肯特·貝克(Kent Beck)的“ TDD以身作則”: https : //www.amazon.co.uk/Test-Driven-Development-Addison-Wesley-Signature/dp/0321146530
一種方法是使用一些內存數據庫(在Spring Boot的情況下,自然選擇H2)。 然后,您可以創建schema.sql
和data.sql
腳本插入特定的數據來測試您的極端情況。
另一個選擇是使用Mockito模擬EntityManager
和Query
對象。 然后,您不需要使用插入和“創建表”來創建SQL,但是您可以手動准備一組對象,這些對象將由這些模擬返回。
希望這可以幫助。 如果您想讓我詳細說明一下,請告訴我。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.