如何编写功能测试

时间:2020-06-09 11:51:04

标签: go functional-testing wiremock

我需要测试Kafka使用者方法,该方法又会创建对另一个服务的调用。如何为另一项服务编写Wiremock。代码是:

  1. Kafka消费者
func (handler FxmWorklogEventHandler) ProcessMessage(message kafka.Message) (swgykafka.Status, error) {
    txn := util.TraceTransaction(handler.MessageType())
    if txn != nil {
        defer txn.End()
    }
    var fxmWorklogEvent event.FxmWorklogEvent
    err := json.Unmarshal(message.Data, &fxmWorklogEvent)
    if err != nil {
        log.Error().Err(err).Msg("FxmWorklogEventHandler - Failed to un marshall the message")
        return swgykafka.HardFailure, err
    }
    log.Info().Msgf("Processing fxm worklog event for order %s", fxmWorklogEvent.OrderId)
    fxmEventToPersist := BuildFxmEventToPersist(fxmWorklogEvent)
    _, err = handler.am.SaveFxmWorklog(fxmEventToPersist)
    if err != nil {
        log.Error().Err(err).Msgf("Failed to save fxm worklog event in am for order %s", fxmWorklogEvent.OrderId)
        return swgykafka.SoftFailure, nil
    }

    return swgykafka.Success, nil
}
  1. 发帖
func (am *AssistanceManagerClient) SaveFxmWorklog(r request.FxmWorklogDetails) (*response.BaseResponse, error) {
    url := config.Client.GetString(amHost) + util.AssistanceManagerSaveFxmWorklog
    return am.hc.Post(url, r, http.Header{}, util.AssistanceManagerCommand)
}
  1. 外部服务方法
func SaveFxmWorklog(w http.ResponseWriter, r *http.Request) {
    var rb entity.FxmWorklogDetails
    err := json.NewDecoder(r.Body).Decode(&rb)
    if err != nil {
        log.Error().Err(err).Msgf("Failed to parse the fxm worklog request %s", util.ToJSON(rb))
        util.ErrorResponse(w, err.Error(), http.StatusBadRequest)
        return
    }
    log.Info().Msgf("Save fxm worklog request received for order %s", rb.OrderId)
    response, err := service.FxmService.SaveFxmWorklogEvent(rb)
    if err != nil {
        log.Error().Err(err).Msgf("Failed to save fxm worklog event %s", util.ToJSON(rb))
        util.ErrorResponse(w, err.Error(), http.StatusInternalServerError)
        return
    }
    w.WriteHeader(response.HTTPStatusCode)
    _ = json.NewEncoder(w).Encode(response)
}

如何通过模拟外部服务api调用来测试此方法?

0 个答案:

没有答案