【问题标题】:How to Write the test case for DAO class with JUNIT5 and mockito in spring boot for the code如何在 Spring Boot 中使用 JUNIT5 和 mockito 为代码编写 DAO 类的测试用例
【发布时间】:2021-11-01 07:45:40
【问题描述】:

如何在 Spring Boot 中使用 JUnit5 和 Mockito 为代码编写 DAO 类的测试用例

我正在尝试使用 JUnit5 和 Mockito 编写测试用例。如何为以下方法编写测试用例

DAO 类:

public Map<String, Object> addParticipantSubRole(ProductLineParticipantSubRoleDTO subRole, int userId, int buId,int plId) throws RTDataBaseException {
        Map<String, Object> returnMap = new HashMap<>();

        try {

            StoredProcedureQuery query = this.getSession()
                    .createStoredProcedureCall("PKG_QA_PRODUCT_LINE_ADMIN.PROC_ADD_PARTICIP_SUB_ROLES")
                    .registerStoredProcedureParameter(1, Integer.class, ParameterMode.IN).setParameter(1, userId)
                    .registerStoredProcedureParameter(2, Integer.class, ParameterMode.IN).setParameter(2, plId)
                    .registerStoredProcedureParameter(3, String.class, ParameterMode.IN)
                    .setParameter(3, subRole.getParticipantSubRoleName())
                    .registerStoredProcedureParameter(4, String.class, ParameterMode.IN)
                    .setParameter(4, subRole.getParticipantSubRoleDesc())
                    .registerStoredProcedureParameter(5, Integer.class, ParameterMode.OUT)
                    .registerStoredProcedureParameter(6, Integer.class, ParameterMode.OUT)
                    .registerStoredProcedureParameter(7, String.class, ParameterMode.OUT)
                    .registerStoredProcedureParameter(8, String.class, ParameterMode.OUT);

            int plParticipantSubRoleId = (Integer) query.getOutputParameterValue(5);
            int returnId = (Integer) query.getOutputParameterValue(6);
            String message = (String) query.getOutputParameterValue(7);

            if (returnId == Constants.SUCCESS_INTEGER_VALUE) {
                returnMap.put(RESULT, plParticipantSubRoleId);
                returnMap.put(RETURN_ID, returnId);
                returnMap.put(RETURN_MESSAGE, message);
            } else {
                returnMap.put(RESULT, subRole);
                returnMap.put(RETURN_ID, returnId);
                returnMap.put(RETURN_MESSAGE, message);
            }

        } catch (Exception exception) {
            log.error(exception.toString());
            throw new RTDataBaseException(userId, "Failed to add PL Sub Role in DB", userId, exception);
        }
        return returnMap;
    }

【问题讨论】:

  • 简单的答案是:不要走这条路。你会得到很多无法维护的代码而没有什么好处。针对数据库实例进行测试:在测试期间在容器中启动(您可以使用 testcontainers 来帮助)或内存数据库。
  • +1 到上面的评论。在您的测试中使用 @DataJpaTest 注释并针对实际数据库运行您的代码,无论是内存中的还是真实的。

标签: spring-boot mockito junit5


【解决方案1】:

在持久层上进行单元测试几乎是零意义。你会得到很多代码,收益几乎为零。

最好进行如下所示的集成测试...

@DataJpaTest
class MyDaoTest {

  @Autowired private MyDao myDao;

  @Test
  void testMyDao(){
     // Do test setup
     myDay.addParticipantSubRole(/* input parameters */)
    // Do asserts

  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    • 2022-06-23
    • 2022-01-23
    • 2021-07-26
    • 2021-11-12
    • 2019-05-24
    相关资源
    最近更新 更多