Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 生产中的Spring Boot API模拟_Java_Spring Boot_Production Environment_Demo_Embedded Server - Fatal编程技术网

Java 生产中的Spring Boot API模拟

Java 生产中的Spring Boot API模拟,java,spring-boot,production-environment,demo,embedded-server,Java,Spring Boot,Production Environment,Demo,Embedded Server,我知道,我们可以在Spring Boot中轻松地在测试范围内模拟代码。在这里,我想尝试在SpringBoot中创建一个演示生产范围/概要文件。在这个配置文件中,我想使用模拟场景 例如,在我的代码中,有第三方API调用: String API_URL = "https://external.com/v1/%s"; private CloseableHttpClient httpClient; public Result executeRequest(String apiVersion, Strin

我知道,我们可以在Spring Boot中轻松地在测试范围内模拟代码。在这里,我想尝试在SpringBoot中创建一个演示生产范围/概要文件。在这个配置文件中,我想使用模拟场景

例如,在我的代码中,有第三方API调用:

String API_URL = "https://external.com/v1/%s";
private CloseableHttpClient httpClient;
public Result executeRequest(String apiVersion, String subUrl, HttpMethod httpMethod)
{
    try
    {
        HttpRequestBase httpRequest;
        String url = String.format(API_URL, subUrl);
        if (httpMethod.equals(HttpMethod.GET))
        {
            httpRequest = new HttpGet(url);
        }
        else if (httpMethod.equals(HttpMethod.POST))
        {
            httpRequest = new HttpPost(url);
            ((HttpPost) httpRequest).setEntity(new StringEntity(requestBody, "UTF-8"));
        }
        ...
        headers.forEach(httpRequest::setHeader);
        HttpResponse response = httpClient.execute(httpRequest);
    }
    catch (IOException e)
    {
        logger.error("IO Error: {}", e.getMessage());
        return handleExceptions(e);
    }
} 
有没有办法在生产中模仿它?或者更好的方式;有没有一种方法可以像wiremock一样为它创建嵌入式服务器


注意:我已经在我的项目上实现了不同的配置文件属性,如(生产、测试和开发),因此这与使用不同的配置文件无关。在这里,我只想在生产环境中模拟API,而不是在测试概要文件中。当然,对于演示配置文件,我将创建demo.properties

解决方案1:

可以通过以下配置实现行为

@Configuration
@Profile("!demo")
public class ProductionConfiguration {

    // Real configuration
}


@Configuration
@Profile("demo")
public class ProductionConfiguration {

    // Mock configuration
}
由于
@MockBean
注释是spring测试依赖项的一部分,因此在部署应用程序时它将不可用。您需要自己创建mock
Type mockObj=Mockito.mock(Type.class)

但这需要将
mockito
依赖项打包为生成工件的一部分

解决方案2:(推荐)

  • 将API URL外部化到属性文件
  • 创建一个单独的属性文件
    application demo.properties
    ,用于演示
  • 更新此属性文件中的URL以连接到外部
    WireMock
    服务
这确保了您的生产工件不需要包含仅用于演示目的的不必要的依赖项

如果需要,当
demo
配置文件处于活动状态时,您可以选择启动嵌入式
WireMock
服务器。但这意味着相关依赖项必须是依赖项的一部分,或者在类路径中可用。如果可能,最好将WireMock作为外部服务运行


解决方案2的示例代码可供您参考。

Sudhir感谢您的回答,但我的项目中已经有不同的配置文件属性,如(生产、测试和开发),因此这与使用不同的配置文件无关。在这里,我只想在生产环境中模拟API,而不是测试概要文件。所以,如果你声称Wiremock在这里有效,你能给我举个例子吗?@Sha我已经做了一个示例应用程序来演示这一点。您可以访问它,Sudhir谢谢您的回答。请根据您的评论/代码更新您的答案。然后我会批准它。@Sha我提供的代码示例基于我的答案中的解决方案2(但使用嵌入式
WireMock
server)。我可以知道为什么你认为它与我的答案不同吗?请把它链接到你的答案上;示例代码:GitHub链接。。。