小编End*_*ika的帖子

Spring MockRestServiceServer处理多个异步请求

我有一个Orchestrator Spring Boot服务,该服务对外部服务发出几个异步的REST请求,我想模拟那些服务的响应。

我的代码是:

 mockServer.expect(requestTo("http://localhost/retrieveBook/book1"))
    .andExpect(method(HttpMethod.GET))
    .andRespond(MockRestResponseCreators.withStatus(HttpStatus.OK)
        .body("{\"book\":{\"title\":\"xxx\",\"year\":\"2000\"}}")
            .contentType(MediaType.APPLICATION_JSON));

mockServer.expect(requestTo("http://localhost/retrieveFilm/film1"))
    .andExpect(method(HttpMethod.GET))
    .andRespond(MockRestResponseCreators.withStatus(HttpStatus.OK)
        .body("{\"film\":{\"title\":\"yyy\",\"year\":\"1900\"}}")
            .contentType(MediaType.APPLICATION_JSON));

service.retrieveBookAndFilm(book1,film1);
        mockServer.verify();
Run Code Online (Sandbox Code Playgroud)

resolveBookAndFilm服务调用两种异步方法,一种方法是检索书本,另一种方法是检索电影,问题在于有时会先执行电影服务,但会出现错误:

java.util.concurrent.ExecutionException:java.lang.AssertionError:请求URI预期:HTTP://本地主机/ retrieveBook / BOOK1却被:HTTP://本地主机/ retrieveFilm / FILM1

任何想法我怎么解决这个问题,有什么类似mockito的说法,当执行此URL然后返回this或那个?

感谢和问候

junit spring-boot mockserver

4
推荐指数
1
解决办法
2800
查看次数

Angular 7 HTTP GET发送JSON对象作为参数

我试图通过角度将JSON结构发送到REST服务

  let test5var = {
                    "test5var1": {
                        "test5var2": "0317",
                        "test5var3": "9556"
                    },
                    "test5var4": "123",
                    "test5var": "0000046"
                }
let dataPrincipalBlnc = {"test": {"test1": {"test2": "0317","test3": {"IDIOMA_ISO": " en","DIALECTO_ISO": "US"},"channel": "INT"},"input": {"test5": test5var}}};

let headers = new HttpHeaders();
    headers.append('Content-Type', 'application/json');

    let params = new HttpParams().set("requestData", dataPrincipalBlnc.toString()).set("authenticationType", this.authType);


    return this.http.get(this.url, {params: params}); 
Run Code Online (Sandbox Code Playgroud)

请求的结果应如下所示:

https://example.com/test?authenticationType=cookie&requestData=%7B%test%22:%7B%22test1%22:%7B%22test2%22:%220317%22,%22test3%22:%7B%22IDIOMA_ISO% 22:%22 + en%22,%22DIALECTO_ISO%22:%22US%22%7D,%22channel%22:%22INT%22%7D,%22input%22:%7B%22test5%22:%7B%22test5var1% 22:%7B%22test5var2%22:%220317%22,%22test5var3%22:%229556%22%7D,%22test5var4%22:%22123%22,%22test5var5%22:%220000986%22%7D%7D% 7D%7D

但当前发送为:

https://example.com/test?requestData=%5Bobject%20Object%5D&authenticationType=cookie

有什么想法可以将json对象发送为第一个请求吗?我需要手动将json转换为有效的uri格式吗?

在angularJS中,仅使用以下代码即可正常工作:

var data = {
      "test1": {
        "test2": {
          "test3": "0317",
          "test4": {
            "IDIOMA_ISO": " en",
            "DIALECTO_ISO": "US"
          },
          "channel": "INT" …
Run Code Online (Sandbox Code Playgroud)

json get http angular

3
推荐指数
1
解决办法
9540
查看次数

标签 统计

angular ×1

get ×1

http ×1

json ×1

junit ×1

mockserver ×1

spring-boot ×1