国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

Feign Stub擋板和Mock

Worktile / 2785人閱讀

摘要:退出擋板模式登錄驗證碼,請不要對非本人透露。運行之前寫的單元測試代碼輸入一個錯誤的手機號進入擋板模式登錄驗證碼,請不要對非本人透露。

背景:

在項目開發中,會有調用第三方接口的場景。當開發時,對方不愿意提供測試服務器給我們調用,或者有的接口會按調用次數進行計費。當聯調時,第三方的測試服務器也可能會出現不穩定,如果他們的服務掛了,我們就一直等著服務恢復,那么這就相當影響效率了。如果我們在開發時,就定義一個擋板或者mock服務,在發起調用時,不直接調到第三方接口,而是調到我們自己的擋板代碼或者mock服務,這樣就可以避免這些問題了。

優勢:

擋板代碼,不需要侵入業務代碼,可以根據入參做一些動態結果返回

不需要專門開發一個擋板服務,并且在每次啟動客戶端都先啟動擋板服務

可以自由選擇使用擋板還是Mock數據

Demo詳細代碼,已經提交到Github,歡迎star

Demo地址: https://github.com/Seifon/Fei...

一、下面我就以一個第三方SMS短信接口來做演示:

首先,我們寫一個Feign客戶端接口,正常調用第三方接口:

1.定義一個SMS短信的Feign客戶端接口:
import cn.seifon.example.feignstubmock.dto.YunxunSmsReqDto;
import cn.seifon.example.feignstubmock.dto.YunxunSmsRespDto;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * @Author: Seifon
 * @Description:
 * @Date: Created in 10:24 2019/1/7
 */
@FeignClient(name = "smsclient", url = "${sms.url}", primary = false)
public interface YunxunSmsFeign {

    /**
     *
     * @param request
     * @return {"code":"0","failNum":"0","successNum":"1","msgId":"19012516213625881","time":"20190125162136","errorMsg":""}
     * @return {"code":"107","msgId":"","time":"20190125162358","errorMsg":"手機號碼格式錯誤"}
     */
    @PostMapping("/msg/variable/json")
    YunxunSmsRespDto send(@RequestBody YunxunSmsReqDto request);
}
注意:@FeignClient注解里面的primary屬性一定要設置為false,這是為了防止在開啟Feign擋板時,出現多個Feign客戶端導致啟動報錯。
2.寫一個單元測試:
import cn.seifon.example.feignstubmock.dto.YunxunSmsReqDto;
import cn.seifon.example.feignstubmock.dto.YunxunSmsRespDto;
import cn.seifon.example.feignstubmock.feign.YunxunSmsFeign;
import com.alibaba.fastjson.JSON;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class FeignStubMockApplicationTests {
    @Autowired
    private YunxunSmsFeign yunxunSmsFeign;

    @Test
    public void feignStubMockTest() {
        YunxunSmsReqDto yunxunSmsReqDto=new YunxunSmsReqDto();
        yunxunSmsReqDto.setAccount("XXXXXXX");
        yunxunSmsReqDto.setPassword("XXXXXXX");
        yunxunSmsReqDto.setMsg("登錄驗證碼:{$var},請不要對非本人透露。");
        yunxunSmsReqDto.setParams("13011112222,123456");
        yunxunSmsReqDto.setReport("true");

        YunxunSmsRespDto send = yunxunSmsFeign.send(yunxunSmsReqDto);
        
        //打印結果
        System.out.println(JSON.toJSON(send));
    }

}
3.1.我們輸入一個正確的手機號,拿一個成功的結果:
2019-01-28 11:17:56.718 DEBUG 6920 --- [           main] c.s.e.f.feign.YunxunSmsFeign             : [YunxunSmsFeign#send] ---> POST http://smssh1.253.com/msg/variable/json HTTP/1.1
2019-01-28 11:17:56.719 DEBUG 6920 --- [           main] c.s.e.f.feign.YunxunSmsFeign             : [YunxunSmsFeign#send] Content-Type: application/json;charset=UTF-8
2019-01-28 11:17:56.720 DEBUG 6920 --- [           main] c.s.e.f.feign.YunxunSmsFeign             : [YunxunSmsFeign#send] Content-Length: 160
2019-01-28 11:17:56.720 DEBUG 6920 --- [           main] c.s.e.f.feign.YunxunSmsFeign             : [YunxunSmsFeign#send] 
2019-01-28 11:17:56.721 DEBUG 6920 --- [           main] c.s.e.f.feign.YunxunSmsFeign             : [YunxunSmsFeign#send] {"account":"XXXXXX","password":"XXXXXXX","msg":"登錄驗證碼:{$var},請不要對非本人透露。","params":"17311112222,123456","report":"true"}
2019-01-28 11:17:56.721 DEBUG 6920 --- [           main] c.s.e.f.feign.YunxunSmsFeign             : [YunxunSmsFeign#send] ---> END HTTP (160-byte body)
2019-01-28 11:17:56.958 DEBUG 6920 --- [           main] c.s.e.f.feign.YunxunSmsFeign             : [YunxunSmsFeign#send] <--- HTTP/1.1 200 OK (236ms)
2019-01-28 11:17:56.960 DEBUG 6920 --- [           main] c.s.e.f.feign.YunxunSmsFeign             : [YunxunSmsFeign#send] connection: keep-alive
2019-01-28 11:17:56.962 DEBUG 6920 --- [           main] c.s.e.f.feign.YunxunSmsFeign             : [YunxunSmsFeign#send] content-length: 109
2019-01-28 11:17:56.963 DEBUG 6920 --- [           main] c.s.e.f.feign.YunxunSmsFeign             : [YunxunSmsFeign#send] content-type: application/json;charset=UTF-8
2019-01-28 11:17:56.965 DEBUG 6920 --- [           main] c.s.e.f.feign.YunxunSmsFeign             : [YunxunSmsFeign#send] date: Mon, 28 Jan 2019 03:17:56 GMT
2019-01-28 11:17:56.966 DEBUG 6920 --- [           main] c.s.e.f.feign.YunxunSmsFeign             : [YunxunSmsFeign#send] 
2019-01-28 11:17:56.971 DEBUG 6920 --- [           main] c.s.e.f.feign.YunxunSmsFeign             : [YunxunSmsFeign#send] {"code":"0","failNum":"0","successNum":"1","msgId":"19012811175621982","time":"20190128111756","errorMsg":""}
2019-01-28 11:17:56.972 DEBUG 6920 --- [           main] c.s.e.f.feign.YunxunSmsFeign             : [YunxunSmsFeign#send] <--- END HTTP (109-byte body)
{"code":"0","failNum":"0","successNum":"1","msgId":"19012811175621982","time":"20190128111756","errorMsg":""}

此時,我們可以根據日志,看到請求的地址也是第三方的url

3.2.我們輸入一個錯誤的手機號,拿一個失敗的結果:
2019-01-28 11:21:15.300 DEBUG 5288 --- [           main] c.s.e.f.feign.YunxunSmsFeign             : [YunxunSmsFeign#send] ---> POST http://smssh1.253.com/msg/variable/json HTTP/1.1
2019-01-28 11:21:15.301 DEBUG 5288 --- [           main] c.s.e.f.feign.YunxunSmsFeign             : [YunxunSmsFeign#send] Content-Type: application/json;charset=UTF-8
2019-01-28 11:21:15.302 DEBUG 5288 --- [           main] c.s.e.f.feign.YunxunSmsFeign             : [YunxunSmsFeign#send] Content-Length: 152
2019-01-28 11:21:15.302 DEBUG 5288 --- [           main] c.s.e.f.feign.YunxunSmsFeign             : [YunxunSmsFeign#send] 
2019-01-28 11:21:15.303 DEBUG 5288 --- [           main] c.s.e.f.feign.YunxunSmsFeign             : [YunxunSmsFeign#send] {"account":"XXXXX","password":"XXXXXXX","msg":"登錄驗證碼:{$var},請不要對非本人透露。","params":"173,123456","report":"true"}
2019-01-28 11:21:15.303 DEBUG 5288 --- [           main] c.s.e.f.feign.YunxunSmsFeign             : [YunxunSmsFeign#send] ---> END HTTP (152-byte body)
2019-01-28 11:21:15.470 DEBUG 5288 --- [           main] c.s.e.f.feign.YunxunSmsFeign             : [YunxunSmsFeign#send] <--- HTTP/1.1 200 OK (165ms)
2019-01-28 11:21:15.471 DEBUG 5288 --- [           main] c.s.e.f.feign.YunxunSmsFeign             : [YunxunSmsFeign#send] connection: keep-alive
2019-01-28 11:21:15.473 DEBUG 5288 --- [           main] c.s.e.f.feign.YunxunSmsFeign             : [YunxunSmsFeign#send] content-length: 87
2019-01-28 11:21:15.474 DEBUG 5288 --- [           main] c.s.e.f.feign.YunxunSmsFeign             : [YunxunSmsFeign#send] content-type: application/json;charset=UTF-8
2019-01-28 11:21:15.476 DEBUG 5288 --- [           main] c.s.e.f.feign.YunxunSmsFeign             : [YunxunSmsFeign#send] date: Mon, 28 Jan 2019 03:21:15 GMT
2019-01-28 11:21:15.477 DEBUG 5288 --- [           main] c.s.e.f.feign.YunxunSmsFeign             : [YunxunSmsFeign#send] 
2019-01-28 11:21:15.483 DEBUG 5288 --- [           main] c.s.e.f.feign.YunxunSmsFeign             : [YunxunSmsFeign#send] {"code":"107","msgId":"","time":"20190128112115","errorMsg":"手機號碼格式錯誤"}
2019-01-28 11:21:15.484 DEBUG 5288 --- [           main] c.s.e.f.feign.YunxunSmsFeign             : [YunxunSmsFeign#send] <--- END HTTP (87-byte body)
{"code":"107","msgId":"","time":"20190128112115","errorMsg":"手機號碼格式錯誤"}

當我們知道了兩種情況下出現的結果,那么我們就可以模擬響應結果啦。小技巧:我們可以先跟對方調接口,把各種響應報文保存下來,方便后面直接mock數據

二、接下來進入擋板編寫環節:
1.編寫一個YunxunSmsFeignStub類,并實現YunxunSmsFeign接口:
import cn.seifon.example.feignstubmock.dto.YunxunSmsReqDto;
import cn.seifon.example.feignstubmock.dto.YunxunSmsRespDto;
import cn.seifon.example.feignstubmock.feign.YunxunSmsFeign;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author: Seifon
 * @Description:
 * @Date: Created in 10:24 2019/1/7
 */
@Primary //注意:需要在原Feign接口@FeignClient注解加入primary = false 屬性
@Component
@ConditionalOnProperty(name = "feign-stub.yunxun.sms.mode", havingValue = "stub")
public class YunxunSmsFeignStub implements YunxunSmsFeign {
    private static final Logger LOG = LoggerFactory.getLogger(YunxunSmsFeignStub.class);

    @Override
    public YunxunSmsRespDto send(YunxunSmsReqDto request) {
        YunxunSmsRespDto yunxunSmsRespDto = new YunxunSmsRespDto();

        //模擬正常響應結果
        yunxunSmsRespDto.setCode("0");
        yunxunSmsRespDto.setFailNum("0");
        yunxunSmsRespDto.setSuccessNum("1");
        yunxunSmsRespDto.setMsgId(String.valueOf(RandomUtils.nextLong(19000000000000000L, 19999999999999999L)));
        yunxunSmsRespDto.setTime(DateFormatUtils.format(new Date(), "yyyyMMddHHmmss"));
        yunxunSmsRespDto.setErrorMsg("");

        String params = request.getParams();
        String[] paramSplit = StringUtils.split(params, ",");
        if (paramSplit[0].length() != 11) {
            //模擬錯誤響應結果
            yunxunSmsRespDto.setCode("107");
            yunxunSmsRespDto.setMsgId("");
            yunxunSmsRespDto.setErrorMsg("手機號碼格式錯誤");
        }
        return yunxunSmsRespDto;
    }
}
注意:必須標注@Primary注解,否則啟動會報錯。@ConditionalOnProperty的作用就是根據application.yaml配置的相關屬性,判斷是否注入Spring容器
2.application.yaml文件,加入下面的配置:
sms:
    url: "http://smssh1.253.com"

#yunxun:代表第三方系統名稱,sms:代表業務名稱,mode:代表Stub模式,url:代表mock服務地址
feign-stub:
    yunxun:
        sms:
            mode: "stub"
3.為了區分返回的內容是擋板結果,我們可以寫一個AOP切面打印日志:
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @Author: Seifon
 * @Description:
 * @Date: Created in 10:24 2019/1/7
 */
@Aspect
@Component
public class FeignStubAspect {

    private static final Logger LOG = LoggerFactory.getLogger(FeignStubAspect.class);

    @Pointcut("execution(* cn.seifon.example.feignstubmock..stub.*.*(..))")
    public void pointCut(){}

    @Around("pointCut()")
    public Object around(ProceedingJoinPoint pjp){
        String name = StringUtils.join(pjp.getTarget().getClass().getName(), ".", pjp.getSignature().getName());
        LOG.info("-----【{}】---- 進入擋板模式... request: 【{}】", name, JSON.toJSON(pjp.getArgs()));
        try {
            Object proceed = pjp.proceed();
            LOG.info("-----【{}】---- 退出擋板模式... request: 【{}】, response: 【{}】", name, JSON.toJSON(pjp.getArgs()), JSON.toJSON(proceed));
            return proceed;
        } catch (Throwable e) {
            e.printStackTrace();
        }

        return null;
    }

}
4.1.運行之前寫的單元測試代碼(輸入一個正確的手機號):
2019-01-28 11:32:51.255  INFO 7488 --- [           main] c.s.e.f.aspect.FeignStubAspect           : -----【cn.seifon.example.feignstubmock.feign.stub.YunxunSmsFeignStub.send】---- 進入擋板模式... request: 【[{"msg":"登錄驗證碼:{$var},請不要對非本人透露。","password":"XXXXXXX","report":"true","params":"13011112222,123456","account":"XXXXXXX"}]】
2019-01-28 11:32:51.975  INFO 7488 --- [           main] c.s.e.f.aspect.FeignStubAspect           : -----【cn.seifon.example.feignstubmock.feign.stub.YunxunSmsFeignStub.send】---- 退出擋板模式... request: 【[{"msg":"登錄驗證碼:{$var},請不要對非本人透露。","password":"XXXXXXX","report":"true","params":"13011112222,123456","account":"XXXXXXX"}]】, response: 【{"code":"0","failNum":"0","successNum":"1","msgId":"19148964234899564","time":"20190128113251","errorMsg":""}】
{"code":"0","failNum":"0","successNum":"1","msgId":"19148964234899564","time":"20190128113251","errorMsg":""}
4.2.運行之前寫的單元測試代碼(輸入一個錯誤的手機號):
2019-01-28 11:35:27.177  INFO 15204 --- [           main] c.s.e.f.aspect.FeignStubAspect           : -----【cn.seifon.example.feignstubmock.feign.stub.YunxunSmsFeignStub.send】---- 進入擋板模式... request: 【[{"msg":"登錄驗證碼:{$var},請不要對非本人透露。","password":"XXXXXXX","report":"true","params":"130,123456","account":"XXXXXXX"}]】
2019-01-28 11:35:27.900  INFO 15204 --- [           main] c.s.e.f.aspect.FeignStubAspect           : -----【cn.seifon.example.feignstubmock.feign.stub.YunxunSmsFeignStub.send】---- 退出擋板模式... request: 【[{"msg":"登錄驗證碼:{$var},請不要對非本人透露。","password":"XXXXXXX","report":"true","params":"130,123456","account":"XXXXXXX"}]】, response: 【{"code":"107","failNum":"0","successNum":"1","msgId":"","time":"20190128113527","errorMsg":"手機號碼格式錯誤"}】
{"code":"107","failNum":"0","successNum":"1","msgId":"","time":"20190128113527","errorMsg":"手機號碼格式錯誤"}

以上代碼就完成了一個stub擋板功能,可有時候,我們已經拿到第三方接口的返回報文,并切不想去寫一大段Stub代碼。那么這個時候,我們就可以選擇下面的Mock方式去完成我們的功能。

三、接下來進入Mock環節:
1. 首先準備一個mock服務,這里我就用自己比較喜歡的一個mock工具(mock-json-server)給大家演示:
1.1 安裝nodejs:
參看官網:http://nodejs.cn/
1.2 安裝mock-json-server:
npm install -g mock-json-server
1.3 新建mock數據文件(命名為:data.json):
{
  "/msg/variable/json": {
    "post": {
      "code":"0",
      "failNum":"0",
      "successNum":"1",
      "msgId":"19012516213625881",
      "time":"20190125162136",
      "errorMsg":""
    }
  }
}
1.4 運行:
mock-json-server {path}/data.json --port=1240

{path}替換為存放data.json的絕對路徑
1.5 如果顯示如下結果,就代表mock服務運行成功:
JSON Server running at http://localhost:1240/
mock-json-server具體使用文檔,請參考:https://www.npmjs.com/package...
2. 準備工作做好后,接下來,就進入Mock正式環節:
2.1 首先,我們定義一個YunxunSmsFeignMock接口,并且繼承YunxunSmsFeign接口
import cn.seifon.example.feignstubmock.feign.YunxunSmsFeign;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

/**
 * @Author: Seifon
 * @Description:
 * @Date: Created in 10:24 2019/1/7
 */
@Primary //注意:需要在原Feign接口@FeignClient注解加入primary = false 屬性
@Component
@ConditionalOnProperty(name = "feign-stub.yunxun.sms.mode", havingValue = "mock")
@FeignClient(name = "smsclient-mock", url = "${feign-stub.yunxun.sms.mockUrl}" ,path = "/")
public interface YunxunSmsFeignMock extends YunxunSmsFeign {

}

注意:必須標注@Primary注解,否則啟動時會報錯。@FeignClient里的name屬性不能跟原Feign接口名稱相同,如果相同會啟動報錯。@ConditionalOnProperty的作用就是根據application.yaml配置的相關屬性,判斷是否注入Spring容器

2.2 application.yaml文件,加入下面的配置:
sms:
    url: "http://smssh1.253.com"

#生產環境請勿添加此配置。mode說明:""-不開啟, "mock"-mock模式, "stub"-stub模式。url說明:只有mock模式需要配置調試url。fund為第三方機構,repayment是業務名稱
#yunxun:代表第三方系統名稱,sms:代表業務名稱,mode:代表擋板模式,url:代表mock服務地址
feign-stub:
    yunxun:
        sms:
            mode: "mock"
            mockUrl: "http://localhost:1240"
2.3 為了區分返回的內容是Mock結果,我們可以寫一個AOP切面打印日志:
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @Author: Seifon
 * @Description:
 * @Date: Created in 10:24 2019/1/7
 */
@Aspect
@Component
public class FeignMockAspect {

    private static final Logger LOG = LoggerFactory.getLogger(FeignMockAspect.class);

    @Pointcut("execution(* cn.seifon.example.feignstubmock..mock.*.*(..))")
    public void pointCut(){}

    @Around("pointCut()")
    public Object around(ProceedingJoinPoint pjp){
        String name = StringUtils.join(pjp.getTarget().getClass().getName(), ".", pjp.getSignature().getName());
        LOG.info("-----【{}】---- 進入Mock模式... request: 【{}】", name, JSON.toJSON(pjp.getArgs()));
        try {
            Object proceed = pjp.proceed();
            LOG.info("-----【{}】---- 退出Mock模式... request: 【{}】, response: 【{}】", name, JSON.toJSON(pjp.getArgs()), JSON.toJSON(proceed));
            return proceed;
        } catch (Throwable e) {
            e.printStackTrace();
        }

        return null;
    }

}
2.4 運行之前的單元測試類,得到如下結果:
2019-01-28 16:16:35.567  INFO 8976 --- [           main] c.s.e.f.aspect.FeignMockAspect           : -----【com.sun.proxy.$Proxy95.send】---- 進入Mock模式... request: 【[{"msg":"登錄驗證碼:{$var},請不要對非本人透露。","password":"XXXXXXX","report":"true","params":"13011112222,123456","account":"XXXXXXX"}]】
2019-01-28 16:16:35.934 DEBUG 8976 --- [           main] c.s.e.f.feign.mock.YunxunSmsFeignMock    : [YunxunSmsFeignMock#send] ---> POST http://localhost:1240/msg/variable/json HTTP/1.1
2019-01-28 16:16:35.935 DEBUG 8976 --- [           main] c.s.e.f.feign.mock.YunxunSmsFeignMock    : [YunxunSmsFeignMock#send] Content-Type: application/json;charset=UTF-8
2019-01-28 16:16:35.936 DEBUG 8976 --- [           main] c.s.e.f.feign.mock.YunxunSmsFeignMock    : [YunxunSmsFeignMock#send] Content-Length: 152
2019-01-28 16:16:35.936 DEBUG 8976 --- [           main] c.s.e.f.feign.mock.YunxunSmsFeignMock    : [YunxunSmsFeignMock#send] 
2019-01-28 16:16:35.937 DEBUG 8976 --- [           main] c.s.e.f.feign.mock.YunxunSmsFeignMock    : [YunxunSmsFeignMock#send] {"account":"XXXXXXX","password":"XXXXXXX","msg":"登錄驗證碼:{$var},請不要對非本人透露。","params":"13011112222,123456","report":"true"}
2019-01-28 16:16:35.937 DEBUG 8976 --- [           main] c.s.e.f.feign.mock.YunxunSmsFeignMock    : [YunxunSmsFeignMock#send] ---> END HTTP (152-byte body)
2019-01-28 16:16:36.021 DEBUG 8976 --- [           main] c.s.e.f.feign.mock.YunxunSmsFeignMock    : [YunxunSmsFeignMock#send] <--- HTTP/1.1 200 OK (82ms)
2019-01-28 16:16:36.021 DEBUG 8976 --- [           main] c.s.e.f.feign.mock.YunxunSmsFeignMock    : [YunxunSmsFeignMock#send] access-control-allow-origin: *
2019-01-28 16:16:36.022 DEBUG 8976 --- [           main] c.s.e.f.feign.mock.YunxunSmsFeignMock    : [YunxunSmsFeignMock#send] connection: keep-alive
2019-01-28 16:16:36.023 DEBUG 8976 --- [           main] c.s.e.f.feign.mock.YunxunSmsFeignMock    : [YunxunSmsFeignMock#send] content-length: 109
2019-01-28 16:16:36.023 DEBUG 8976 --- [           main] c.s.e.f.feign.mock.YunxunSmsFeignMock    : [YunxunSmsFeignMock#send] content-type: application/json; charset=utf-8
2019-01-28 16:16:36.024 DEBUG 8976 --- [           main] c.s.e.f.feign.mock.YunxunSmsFeignMock    : [YunxunSmsFeignMock#send] date: Mon, 28 Jan 2019 08:16:36 GMT
2019-01-28 16:16:36.024 DEBUG 8976 --- [           main] c.s.e.f.feign.mock.YunxunSmsFeignMock    : [YunxunSmsFeignMock#send] etag: W/"6d-XqhLoZB8r6IRF2Lb6CWoIVVNhIQ"
2019-01-28 16:16:36.025 DEBUG 8976 --- [           main] c.s.e.f.feign.mock.YunxunSmsFeignMock    : [YunxunSmsFeignMock#send] x-content-type-options: nosniff
2019-01-28 16:16:36.026 DEBUG 8976 --- [           main] c.s.e.f.feign.mock.YunxunSmsFeignMock    : [YunxunSmsFeignMock#send] x-powered-by: Express
2019-01-28 16:16:36.027 DEBUG 8976 --- [           main] c.s.e.f.feign.mock.YunxunSmsFeignMock    : [YunxunSmsFeignMock#send] 
2019-01-28 16:16:36.030 DEBUG 8976 --- [           main] c.s.e.f.feign.mock.YunxunSmsFeignMock    : [YunxunSmsFeignMock#send] {"code":"0","failNum":"0","successNum":"1","msgId":"19012516213625881","time":"20190125162136","errorMsg":""}
2019-01-28 16:16:36.030 DEBUG 8976 --- [           main] c.s.e.f.feign.mock.YunxunSmsFeignMock    : [YunxunSmsFeignMock#send] <--- END HTTP (109-byte body)
2019-01-28 16:16:36.227  INFO 8976 --- [           main] c.s.e.f.aspect.FeignMockAspect           : -----【com.sun.proxy.$Proxy95.send】---- 退出Mock模式... request: 【[{"msg":"登錄驗證碼:{$var},請不要對非本人透露。","password":"XXXXXXX","report":"true","params":"13011112222,123456","account":"XXXXXXX"}]】, response: 【{"code":"0","failNum":"0","successNum":"1","msgId":"19012516213625881","time":"20190125162136","errorMsg":""}】
{"code":"0","failNum":"0","successNum":"1","msgId":"19012516213625881","time":"20190125162136","errorMsg":""}

說明:此時我們根據日志,會發現feign調用的url已經變為我們的Mock服務地址了。同理,如果要返回失敗結果,只需要修改data.json文件,再次調用后,即可得到我們想要的結果了。

四、結語:

如果有什么需要改進的地方,或者不正確的地方,請在評論里面提出并指正。謝謝!

Demo詳細代碼,已經提交到Github,歡迎star

Demo地址: https://github.com/Seifon/Fei...

項目結構,如圖:

原文地址:http://www.seifon.cn/2019/01/...

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/73234.html

相關文章

  • Sinon

    摘要:完成后一定要否則會影響其他的測試的作用在于可以監視一個函數被調用的情況。在中用于對一個的活動進行監視。而出的收到了數據或是調用并沒有真正執行,一切針對的調用都是假的。對相關的測試也提供了模擬功能。 Sinon.js是個測試輔助工具,在為Node程序寫測試時可能可以派上用場。 在測試領域有這么幾個基本名詞:spy, stub, mock,這三個概念都是測試所用到的手段。Sinon.js就...

    since1986 評論0 收藏0
  • 【Node Hero】9. Node.js 單元測試

    摘要:基本上,測試金字塔描述你應該編寫單元測試集成測試和端到端測試。集成測試要比端到端測試多,單元測試甚至要更多一些。應用程序單元測試編寫單元測試,是為了看看給定的模塊單元是否工作。 本文轉載自:眾成翻譯譯者:網絡埋伏紀事鏈接:http://www.zcfy.cc/article/1754原文:https://blog.risingstack.com/node-hero-node-js-un...

    104828720 評論0 收藏0
  • iOS測試規范

    摘要:而且,構造函數不應該包含行為,所以沒有值得測試的東西。一旦一個被創建了,你測試它的不為空因為你知道構造函數創建了一個新的并將它賦給了變量。現在為類的構造函數編寫的測試出問題了,即便類運行良好,但并沒有包括。 在研究了有關測試的相關內容,發現有些被忽略掉的很明顯的問題。 1、為什么需要測試 從積極的方面來說,寫自動化測試的優勢如下: 使重構更簡單 —— 你可以自信的修改實現細節,而不用...

    spademan 評論0 收藏0
  • 【測試相關】mockstub的區別

    復習軟工二的時候遇到的占坑考完試回來寫

    Riddler 評論0 收藏0

發表評論

0條評論

最新活動
閱讀需要支付1元查看
<