3 * Copyright 2017 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 package mock_helloworld_test
25 "github.com/golang/mock/gomock"
26 "github.com/golang/protobuf/proto"
27 "golang.org/x/net/context"
28 helloworld "google.golang.org/grpc/examples/helloworld/helloworld"
29 hwmock "google.golang.org/grpc/examples/helloworld/mock_helloworld"
32 // rpcMsg implements the gomock.Matcher interface
37 func (r *rpcMsg) Matches(msg interface{}) bool {
38 m, ok := msg.(proto.Message)
42 return proto.Equal(m, r.msg)
45 func (r *rpcMsg) String() string {
46 return fmt.Sprintf("is %s", r.msg)
49 func TestSayHello(t *testing.T) {
50 ctrl := gomock.NewController(t)
52 mockGreeterClient := hwmock.NewMockGreeterClient(ctrl)
53 req := &helloworld.HelloRequest{Name: "unit_test"}
54 mockGreeterClient.EXPECT().SayHello(
57 ).Return(&helloworld.HelloReply{Message: "Mocked Interface"}, nil)
58 testSayHello(t, mockGreeterClient)
61 func testSayHello(t *testing.T, client helloworld.GreeterClient) {
62 r, err := client.SayHello(context.Background(), &helloworld.HelloRequest{Name: "unit_test"})
63 if err != nil || r.Message != "Mocked Interface" {
64 t.Errorf("mocking failed")
66 t.Log("Reply : ", r.Message)