From 9cc4fd4ccaf4b8e988967534493f93c8b3ff94b1 Mon Sep 17 00:00:00 2001 From: Mahak Mukhi Date: Tue, 13 Dec 2016 13:06:25 -0800 Subject: [PATCH] Example code on how to mock gRPC services --- examples/helloworld/mock/generate_mock.sh | 2 + examples/helloworld/mock/hw_test.go | 49 +++++++++++++++++++ .../mock/mock_helloworld/hw_mock.go | 48 ++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100755 examples/helloworld/mock/generate_mock.sh create mode 100644 examples/helloworld/mock/hw_test.go create mode 100644 examples/helloworld/mock/mock_helloworld/hw_mock.go diff --git a/examples/helloworld/mock/generate_mock.sh b/examples/helloworld/mock/generate_mock.sh new file mode 100755 index 00000000..2321021c --- /dev/null +++ b/examples/helloworld/mock/generate_mock.sh @@ -0,0 +1,2 @@ +mockgen google.golang.org/grpc/examples/helloworld/helloworld GreeterClient > mock_helloworld/hw_mock.go +gofmt -w mock_helloworld/hw_mock.go diff --git a/examples/helloworld/mock/hw_test.go b/examples/helloworld/mock/hw_test.go new file mode 100644 index 00000000..5c134755 --- /dev/null +++ b/examples/helloworld/mock/hw_test.go @@ -0,0 +1,49 @@ +package mock + +import ( + "fmt" + "testing" + + "github.com/golang/mock/gomock" + proto "github.com/golang/protobuf/proto" + "golang.org/x/net/context" + "google.golang.org/grpc/examples/helloworld/helloworld" + hwmock "google.golang.org/grpc/examples/helloworld/mock/mock_helloworld" +) + +// rpcMsg implements the gomock.Matcher interface +type rpcMsg struct { + msg proto.Message +} + +func (r *rpcMsg) Matches(msg interface{}) bool { + m, ok := msg.(proto.Message) + if !ok { + return false + } + return proto.Equal(m, r.msg) +} + +func (r *rpcMsg) String() string { + return fmt.Sprintf("is %s", r.msg) +} + +func TestSayHello(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + mockGreeterClient := hwmock.NewMockGreeterClient(ctrl) + req := &helloworld.HelloRequest{Name: "unit_test"} + mockGreeterClient.EXPECT().SayHello( + gomock.Any(), + &rpcMsg{msg: req}, + ).Return(&helloworld.HelloReply{Message: "Mocked Interface"}, nil) + testSayHello(t, mockGreeterClient) +} + +func testSayHello(t *testing.T, client helloworld.GreeterClient) { + r, err := client.SayHello(context.Background(), &helloworld.HelloRequest{Name: "unit_test"}) + if err != nil || r.Message != "Mocked Interface" { + t.Errorf("mocking failed") + } + t.Log("Reply : ", r.Message) +} diff --git a/examples/helloworld/mock/mock_helloworld/hw_mock.go b/examples/helloworld/mock/mock_helloworld/hw_mock.go new file mode 100644 index 00000000..14957ed5 --- /dev/null +++ b/examples/helloworld/mock/mock_helloworld/hw_mock.go @@ -0,0 +1,48 @@ +// Automatically generated by MockGen. DO NOT EDIT! +// Source: google.golang.org/grpc/examples/helloworld/helloworld (interfaces: GreeterClient) + +package mock_helloworld + +import ( + gomock "github.com/golang/mock/gomock" + context "golang.org/x/net/context" + grpc "google.golang.org/grpc" + helloworld "google.golang.org/grpc/examples/helloworld/helloworld" +) + +// Mock of GreeterClient interface +type MockGreeterClient struct { + ctrl *gomock.Controller + recorder *_MockGreeterClientRecorder +} + +// Recorder for MockGreeterClient (not exported) +type _MockGreeterClientRecorder struct { + mock *MockGreeterClient +} + +func NewMockGreeterClient(ctrl *gomock.Controller) *MockGreeterClient { + mock := &MockGreeterClient{ctrl: ctrl} + mock.recorder = &_MockGreeterClientRecorder{mock} + return mock +} + +func (_m *MockGreeterClient) EXPECT() *_MockGreeterClientRecorder { + return _m.recorder +} + +func (_m *MockGreeterClient) SayHello(_param0 context.Context, _param1 *helloworld.HelloRequest, _param2 ...grpc.CallOption) (*helloworld.HelloReply, error) { + _s := []interface{}{_param0, _param1} + for _, _x := range _param2 { + _s = append(_s, _x) + } + ret := _m.ctrl.Call(_m, "SayHello", _s...) + ret0, _ := ret[0].(*helloworld.HelloReply) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +func (_mr *_MockGreeterClientRecorder) SayHello(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call { + _s := append([]interface{}{arg0, arg1}, arg2...) + return _mr.mock.ctrl.RecordCall(_mr.mock, "SayHello", _s...) +}