{"id":13694724,"url":"https://github.com/didi/thrift-mock","last_synced_at":"2025-06-15T17:11:02.404Z","repository":{"id":53798057,"uuid":"167303102","full_name":"didi/thrift-mock","owner":"didi","description":"A lightweight java test library for mocking thrift server","archived":false,"fork":false,"pushed_at":"2025-02-12T08:14:14.000Z","size":126,"stargazers_count":93,"open_issues_count":6,"forks_count":31,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-05-03T04:33:49.373Z","etag":null,"topics":["java","mock","server","testing-tools","thrift","unit-test"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/didi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2019-01-24T04:29:28.000Z","updated_at":"2025-04-04T04:01:34.000Z","dependencies_parsed_at":"2025-02-10T07:28:58.320Z","dependency_job_id":"ed15d785-9039-41e3-a612-ede50b1cdc74","html_url":"https://github.com/didi/thrift-mock","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/didi/thrift-mock","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/didi%2Fthrift-mock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/didi%2Fthrift-mock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/didi%2Fthrift-mock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/didi%2Fthrift-mock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/didi","download_url":"https://codeload.github.com/didi/thrift-mock/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/didi%2Fthrift-mock/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260016055,"owners_count":22946321,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["java","mock","server","testing-tools","thrift","unit-test"],"created_at":"2024-08-02T17:01:38.908Z","updated_at":"2025-06-15T17:11:02.384Z","avatar_url":"https://github.com/didi.png","language":"Java","funding_links":[],"categories":["Java","测试"],"sub_categories":[],"readme":"# thrift-mock    \n\n[![Codacy Badge](https://api.codacy.com/project/badge/Grade/41508e6ecd9043e4a797327eb7cbe1cc)](https://app.codacy.com/app/YORYOR/thrift-mock?utm_source=github.com\u0026utm_medium=referral\u0026utm_content=didi/thrift-mock\u0026utm_campaign=Badge_Grade_Dashboard)\n\nA lightweight java unit test library for mocking thrift service\n\n## Features\n* Init thrift mock server without any pre-define thrift idl, only a port is needed\n* Dynamic binding thrift interface to its response\n* Configurable response delays\n* Support binding different interfaces from different thrift service to one server \n* Embedded with junit4\n* Support both blocking and non-blocking call\n\n## Getting Started\n\n### 1. import maven dependency\nworking with junit\n```xml\n    \u003cdependency\u003e\n        \u003cgroupId\u003ecom.didiglobal.thriftmock\u003c/groupId\u003e\n        \u003cartifactId\u003ethrift-mock-server4junit\u003c/artifactId\u003e\n        \u003cversion\u003e1.0.3\u003c/version\u003e\n    \u003c/dependency\u003e\n```\nworking without junit\n```xml\n    \u003cdependency\u003e\n        \u003cgroupId\u003ecom.didiglobal.thriftmock\u003c/groupId\u003e\n        \u003cartifactId\u003ethrift-mock-server\u003c/artifactId\u003e\n        \u003cversion\u003e1.0.3\u003c/version\u003e\n    \u003c/dependency\u003e\n```\nthrift test source\n```xml\n    \u003cdependency\u003e\n        \u003cgroupId\u003ecom.didiglobal.thriftmock\u003c/groupId\u003e\n        \u003cartifactId\u003ethrift-mock-server\u003c/artifactId\u003e\n        \u003cversion\u003e1.0.3\u003c/version\u003e\n    \u003c/dependency\u003e\n```\n\n### 2. start server \n```java\n   //working without junit, blocking server\n   ThriftMockServer server = new ThriftMockServer(9999);\n   Thread t = new Thread(server::start);\n   t.start();\n   \n   //working without junit, starting a non-blocking server\n   AsyncThriftMockServer server = new AsyncThriftMockServer(9999);\n   Thread t = new Thread(server::start);\n   t.start();\n \n \n   //working with junit \n   //blocking server\n   @Rule\n   public ThriftMockServer server = new ThriftMockServer(9999);\n   \n   //non-blocking server\n   @Rule\n   public AsyncThriftMockServer server = new AsyncThriftMockServer(9999);\n\n```  \n### 3. bind interface with expect response   \nIf you have a thrift file like this:\n\n```thrift\nnamespace java com.xiaoju.ddpay.thriftmock.server\n\nstruct Request{\n    1:optional string msg,\n}\n\nstruct Response{\n    1:required i32 code,\n    2:required string responseMsg,\n}\n\n\nservice HelloService {\n    Response sayHello(1:required Request request);\n}\nservice ByeService {\n    Response sayBye(1:required Request request);\n}\n```\n\n\n#### 3.1 bind interface to fixed response\n```java\n    //define expect response\n    Response expectHelloResponse = new Response(200, \"hello\");\n    //bind response\n    server.setExpectReturn(\"sayHello\", expectHelloResponse);\n    //bind response with specific delay(ms)\n    server.setExpectReturn(\"sayHello\", expectHelloResponse, 100);\n```\n#### 3.2 bind interface to dynamic response\n```java\n    //bind interface to dynamic response \n    TBase emptyArgs = new HelloService.sayHello_args();\n    Function\u003cTBase, TBase\u003e returnLogic = (tBase) -\u003e {\n      HelloService.sayHello_args sayHello_args = (HelloService.sayHello_args)tBase;\n      if (sayHello_args.getRequest().getMsg().contains(\"fail\")) {\n        return new Response(500, \"fail\");\n      }\n      return new Response(200, \"success\");\n    };\n    server.setExpectReturn(\"sayHello\", emptyArgs, returnLogic);\n```\n\n#### 3.3 request server\n```java   \n    //init a thrift client connect to mock server \n    TTransport transport = new TSocket(\"127.0.0.1\", 9999);\n    transport.open();\n    TProtocol protocol = new TBinaryProtocol(transport);\n    HelloService.Iface client = new HelloService.Client(protocol);\n    //request interface sayHello\n    Response msg = client.sayHello(request);\n    //get the expect response\n    Assert.assertEquals(msg.getCode(), 200);\n    Assert.assertEquals(msg.getMsg(), \"hello\");\n\n```\n## Todo\n* make it work like wiremock to test thrift service.\n## Contributing    \nWelcome to contribute by creating issues or sending pull requests. See [Contributing Guide](./CONTRIBUTING.md) for guidelines.\n\n## License        \nthrift-mock is licensed under the Apache License 2.0. See the [LICENSE](./LICENSE) file.\n\n## Note    \nThis is not an official Didi product (experimental or otherwise), it is just code that happens to be owned by Didi.\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdidi%2Fthrift-mock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdidi%2Fthrift-mock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdidi%2Fthrift-mock/lists"}