测试 Feign 客户端

时间:2021-07-20 10:43:10

标签: spring-cloud-feign openfeign

我编写了一个假客户端,我想使用单元测试来测试它是否有效。

就我而言,集成测试不是当前开发阶段的正确方法。

伪装客户端是 null,我在运行测试时收到 NullPointerException

如何自动装配?

伪装客户端

package com.myapp.clients;

import com.myapp.model.StatusResponse;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient(name="myClient", url="${feign.clients.my.url}")
public interface myClient {

    @RequestMapping(method= RequestMethod.GET, value="/v1/users/{userId}")
    StatusResponse getStatus(
            @RequestHeader(value = "Auth", required = true) String authorizationHeader,
            @RequestHeader(value = "my_tid", required = true) String tid,
            @PathVariable("userId") String userId);

}

测试:

package com.myapp.clients;

import com.intuit.secfraudshared.step.broker.model.StatusResponse;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

public class MyClientTest {

    @Autowired
    MyClient myClient;
    
    @Test
    public void testmyClient_status200() {

        StatusResponse myResponse = myClient.getStatus("", "tidTestSample", "4626745161770145");
        Assert.assertNotNull(iusResponse);
    }
}

如何自动装配 MyClient?

0 个答案:

没有答案