【问题标题】:How to communicate batch project with Api project?批量项目如何与Api项目通信?
【发布时间】:2022-12-01 02:49:41
【问题描述】:

我有两个项目,第一个称为 api 项目,用于启动 REST api webServices,另一个 project 是负责执行批处理的批处理项目。

如何使批处理项目执行 api 项目中存在的 rest api?

【问题讨论】:

    标签: rest spring-batch


    【解决方案1】:

    使用来自 Postman 的 newman 实用程序用于 batch project。 'newmanis Javascript utility. it can use when production.postman` 是开发测试逻辑时基于 UI 的 API 测试实用程序。

    newman的安装和运行

    它可以从 linux 终端(windows 和 Max 也可以)测试 REST-API 服务。 您可以通过 newman 测试 CRUD(Create/Read/Update/Delete = /Post/Get/Put/Delete) REST APIs

    演示

    #1 为API project测试简单的用户网络服务

    https://jsonplaceholder.typicode.com/users

    [
        {
            "id": 1,
            "name": "Leanne Graham",
            "username": "Bret",
            "email": "Sincere@april.biz",
            "address": {
                "street": "Kulas Light",
                "suite": "Apt. 556",
                "city": "Gwenborough",
                "zipcode": "92998-3874",
                "geo": {
                    "lat": "-37.3159",
                    "lng": "81.1496"
                }
            },
            "phone": "1-770-736-8031 x56442",
            "website": "hildegard.org",
            "company": {
                "name": "Romaguera-Crona",
                "catchPhrase": "Multi-layered client-server neural-net",
                "bs": "harness real-time e-markets"
            }
        },
        {
            "id": 2,
            "name": "Ervin Howell",
            "username": "Antonette",
            "email": "Shanna@melissa.tv",
            "address": {
                "street": "Victor Plains",
                "suite": "Suite 879",
                "city": "Wisokyburgh",
                "zipcode": "90566-7771",
                "geo": {
                    "lat": "-43.9509",
                    "lng": "-34.4618"
                }
            },
            "phone": "010-692-6593 x09125",
            "website": "anastasia.net",
            "company": {
                "name": "Deckow-Crist",
                "catchPhrase": "Proactive didactic contingency",
                "bs": "synergize scalable supply-chains"
            }
        },
        {
            "id": 3,
            "name": "Clementine Bauch",
            "username": "Samantha",
            "email": "Nathan@yesenia.net",
            "address": {
                "street": "Douglas Extension",
                "suite": "Suite 847",
                "city": "McKenziehaven",
                "zipcode": "59590-4157",
                "geo": {
                    "lat": "-68.6102",
                    "lng": "-47.0653"
                }
            },
            "phone": "1-463-123-4447",
            "website": "ramiro.info",
            "company": {
                "name": "Romaguera-Jacobson",
                "catchPhrase": "Face to face bifurcated interface",
                "bs": "e-enable strategic applications"
            }
        }
        ... removed
     ]
    
    

    在 linux 终端通过 newman 测试

    它显示测试结果摘要 我测试了 3 个单元测试

    $ newman run 'Test Users API Service.postman_collection.json' 
    newman
    
    Test Users API Service
    
    → All users
      GET https://jsonplaceholder.typicode.com/users [200 OK, 6.69kB, 773ms]
      ✓  Status code is 200
      ✓  Check the number of users
      ✓  Check the email of Bret
      ✓  Check the name of Bret
      ✓  Check the address of Bret
    
    → User 2
      GET https://jsonplaceholder.typicode.com/users/2 [200 OK, 1.55kB, 205ms]
      ✓  Status code is 200
      ✓  Check the email of Ervin Howell
      ✓  Check the name of Ervin Howell
      ✓  Check the address of Ervin Howell
    
    → User 3
      GET https://jsonplaceholder.typicode.com/users/3 [200 OK, 1.56kB, 194ms]
      ✓  Status code is 200
      ✓  Check the email of Clementine Bauch
      ✓  Check the name of Clementine Bauch
      ✓  Check the address of Clementine Bauch
    
    ┌─────────────────────────┬─────────────────────┬────────────────────┐
    │                         │            executed │             failed │
    ├─────────────────────────┼─────────────────────┼────────────────────┤
    │              iterations │                   1 │                  0 │
    ├─────────────────────────┼─────────────────────┼────────────────────┤
    │                requests │                   3 │                  0 │
    ├─────────────────────────┼─────────────────────┼────────────────────┤
    │            test-scripts │                   3 │                  0 │
    ├─────────────────────────┼─────────────────────┼────────────────────┤
    │      prerequest-scripts │                   0 │                  0 │
    ├─────────────────────────┼─────────────────────┼────────────────────┤
    │              assertions │                  13 │                  0 │
    ├─────────────────────────┴─────────────────────┴────────────────────┤
    │ total run duration: 1253ms                                         │
    ├────────────────────────────────────────────────────────────────────┤
    │ total data received: 6.67kB (approx)                               │
    ├────────────────────────────────────────────────────────────────────┤
    │ average response time: 390ms [min: 194ms, max: 773ms, s.d.: 270ms] │
    └────────────────────────────────────────────────────────────────────┘
    

    这是Postman中的UI测试部分

    它可以在开发期间为每个API开发测试逻辑。

    这个测试逻辑

    #1 检查响应码

    #2 检查用户数量

    #3 检查用户名/电子邮件和地址

    pm.test("Status code is 200", function () {
      pm.response.to.have.status(200);
    });
    
    pm.test('Check the number of users', () => {
        pm.expect(pm.response.json().length).to.equal(10);
    });
    
    let user = pm.response.json().find(a => a.username === 'Bret');
    
    pm.test('Check the email of Bret', () => {
        pm.expect(user.email).to.equal("Sincere@april.biz");
    });
    
    pm.test('Check the name of Bret', () => {
        pm.expect(user.name).to.equal("Leanne Graham");
    });
    
    pm.test('Check the address of Bret', () => {
        pm.expect(user.address.street).to.equal("Kulas Light");
    });
    

    在 Postman 中运行集合

    可以过滤测试项目和循环次数

    显示整个测试步骤和处理时间

    也可以在控制台视图中调试。你可以输入console.log 消息。

    【讨论】:

      猜你喜欢
      • 2014-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 2010-11-26
      相关资源
      最近更新 更多