【问题标题】:Mock connection problems/errors/exceptions模拟连接问题/错误/异常
【发布时间】:2018-08-22 14:54:51
【问题描述】:

我想模拟应用程序在连接到 mock 时出现异常的场景(例如 javax.net.ssl)。我知道如何构建有效或无效的响应。我不知道如何响应 CXF 解释的特定异常,就像异常一样。

简单来说:我想编写示例(或工具),当被询问(通过 get 或 SOAP)时将返回 java 连接异常。

代码说明:

curl http://localhost:8088/myservice

作为响应,我希望抛出连接异常(例如 javax.net.ssl.SSLException ),就像 java 应用程序会抛出一样。我知道如何抛出异常我仍然不知道如何抛出它们以响应请求(例如 GET)。

【问题讨论】:

  • 几段代码比解释好理解
  • 你可以在你想要的地方抛出异常。这就是你想要的吗?也许一点代码会帮助我们。

标签: java testing mocking soapui wiremock


【解决方案1】:

我找到了基于简单 spring boot 应用程序的解决方案 Hello World spring boot 并将其修改为特定的异常(无论如何,只要我们有提供响应的应用或知道框架,它就适用于其他应用):

curl https://start.spring.io/starter.zip \
-d dependencies=web \
-d name=helloworld \
-d artifactId=helloworld \
-o helloworld.zip

解压helloworld.zip

然后粘贴到类中:

import javax.net.ssl.SSLException;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class HelloworldApplication {

    @RestController
    class HelloworldController {
        @GetMapping("/") //this will serve get
        String hello() throws SSLException {
            //this will land in Spring error message text
            throw new SSLException("Tested exception javax.net.ssl");
        }
    }

    public static void main(String[] args) {
        SpringApplication.run(HelloworldApplication.class, args);
    }
}

这将返回一般的 json 错误(在已处理的请求中添加异常的相同技术可以在 servlet 或产生响应的应用程序的其他部分中使用)。

响应将如下所示

{"timestamp":"2018-08-23T09:22:26.691+0000","status":500,"error":"Internal Server Error","message":"Tested exception javax.net.ssl","path":"/"}

【讨论】:

    猜你喜欢
    • 2015-06-05
    • 2012-04-12
    • 1970-01-01
    • 2012-03-09
    • 1970-01-01
    • 2019-10-21
    • 2010-09-21
    • 2017-05-30
    • 2017-09-05
    相关资源
    最近更新 更多