繁体   English   中英

如何在Spring Boot应用程序中测试本地查询?

[英]How to test native queries in Spring Boot application?

通过遵循TDD方法,我在编码方面是全新的。 现在,我不确定每个代码在编写之前是否都需要测试用例。 如何使用if-else条件测试本机查询?

我已经研究了以下一些方法,但并不能满足我的问题:

测试Spring Boot应用程序?

https://stackoverflow.com/search?q=test+entitymanager

https://stackoverflow.com/search?q=entitymanager+returned+null+in+testing

JUnit测试其他情况

@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.sqldata.sql脚本插入特定的数据来测试您的极端情况。

另一个选择是使用Mockito模拟EntityManagerQuery对象。 然后,您不需要使用插入和“创建表”来创建SQL,但是您可以手动准备一组对象,这些对象将由这些模拟返回。

希望这可以帮助。 如果您想让我详细说明一下,请告诉我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM