GithubHelp home page GithubHelp logo

didi / thrift-mock Goto Github PK

View Code? Open in Web Editor NEW
89.0 10.0 30.0 100 KB

A lightweight java test library for mocking thrift server

License: Apache License 2.0

Java 99.26% Thrift 0.74%
mock thrift java testing-tools unit-test server

thrift-mock's Introduction

thrift-mock

Codacy Badge

A lightweight java unit test library for mocking thrift service

Features

  • Init thrift mock server without any pre-define thrift idl, only a port is needed
  • Dynamic binding thrift interface to its response
  • Configurable response delays
  • Support binding different interfaces from different thrift service to one server
  • Embedded with junit4
  • Support both blocking and non-blocking call

Getting Started

1. import maven dependency

working with junit

    <dependency>
        <groupId>com.didiglobal.thriftmock</groupId>
        <artifactId>thrift-mock-server4junit</artifactId>
        <version>1.0.2</version>
    </dependency>

working without junit

    <dependency>
        <groupId>com.didiglobal.thriftmock</groupId>
        <artifactId>thrift-mock-server</artifactId>
        <version>1.0.2</version>
    </dependency>

thrift test source

    <dependency>
        <groupId>com.didiglobal.thriftmock</groupId>
        <artifactId>thrift-mock-server</artifactId>
        <version>1.0.2</version>
    </dependency>

2. start server

   //working without junit, blocking server
   ThriftMockServer server = new ThriftMockServer(9999);
   Thread t = new Thread(server::start);
   t.start();
   
   //working without junit, starting a non-blocking server
   AsyncThriftMockServer server = new AsyncThriftMockServer(9999);
   Thread t = new Thread(server::start);
   t.start();
 
 
   //working with junit 
   //blocking server
   @Rule
   public ThriftMockServer server = new ThriftMockServer(9999);
   
   //non-blocking server
   @Rule
   public AsyncThriftMockServer server = new AsyncThriftMockServer(9999);

3. bind interface with expect response

If you have a thrift file like this:

namespace java com.xiaoju.ddpay.thriftmock.server

struct Request{
    1:optional string msg,
}

struct Response{
    1:required i32 code,
    2:required string responseMsg,
}


service HelloService {
    Response sayHello(1:required Request request);
}
service ByeService {
    Response sayBye(1:required Request request);
}

3.1 bind interface to fixed response

    //define expect response
    Response expectHelloResponse = new Response(200, "hello");
    //bind response
    server.setExpectReturn("sayHello", expectHelloResponse);
    //bind response with specific delay(ms)
    server.setExpectReturn("sayHello", expectHelloResponse, 100);

3.2 bind interface to dynamic response

    //bind interface to dynamic response 
    TBase emptyArgs = new HelloService.sayHello_args();
    Function<TBase, TBase> returnLogic = (tBase) -> {
      HelloService.sayHello_args sayHello_args = (HelloService.sayHello_args)tBase;
      if (sayHello_args.getRequest().getMsg().contains("fail")) {
        return new Response(500, "fail");
      }
      return new Response(200, "success");
    };
    server.setExpectReturn("sayHello", emptyArgs, returnLogic);

3.3 request server

    //init a thrift client connect to mock server 
    TTransport transport = new TSocket("127.0.0.1", 9999);
    transport.open();
    TProtocol protocol = new TBinaryProtocol(transport);
    HelloService.Iface client = new HelloService.Client(protocol);
    //request interface sayHello
    Response msg = client.sayHello(request);
    //get the expect response
    Assert.assertEquals(msg.getCode(), 200);
    Assert.assertEquals(msg.getMsg(), "hello");

Todo

  • make it work like wiremock to test thrift service.

Contributing

Welcome to contribute by creating issues or sending pull requests. See Contributing Guide for guidelines.

License

thrift-mock is licensed under the Apache License 2.0. See the LICENSE file.

Note

This is not an official Didi product (experimental or otherwise), it is just code that happens to be owned by Didi.

thrift-mock's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

thrift-mock's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.