1

需要设置 JMock 代码以使用 google protobuf 测试回调

完整项目位于http://github.com/andrewmilkowski/template-transport

简而言之,以下是方法签名(下)

我需要做的是测试方法getLongValue,使用Jmock JUnit4Mockery

解决这个问题的最好和最干净的方法是什么

非常感谢!

package com.argmaps.client.proto;

import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.fepss.rpc.server.RpcApplication;
import com.fepss.rpc.client.RpcChannelImpl;

import org.apache.tapestry5.ioc.MappedConfiguration;

import com.google.protobuf.RpcController;
import com.google.protobuf.RpcCallback;

import com.argmaps.transport.proto.generated.TransportServer.ProtoService;
import com.argmaps.transport.proto.generated.TransportServer.ProtoService.Stub;
import com.argmaps.transport.proto.generated.TransportServer.DefaultLongValue;
import com.argmaps.transport.proto.generated.TransportServer.LongValue;
import com.argmaps.transport.proto.fepss.ProtoServer.TransportHandler;

public class TransportClient {

    protected final Log LOG = LogFactory.getLog(this.getClass().getName());

    private RpcController controller;

    private TransportHandler transportHandler;
    private ProtoService.Interface service;

    private void open(String host, int port) {

        RpcChannelImpl channel = new RpcChannelImpl(host, port);
     controller = channel.newRpcController();
     service = ProtoService.newStub(channel);

    }

    protected static class LongValueRpcCallback implements RpcCallback<LongValue> {

        private long longValue = 0L;

        @Override
     public void run(LongValue result) {
            longValue = result.getLongValue();
  }

        private long getLongValue() {

            return longValue;
        }
    }

    private void close() {

    }

    public long getLongValue(LongValueRpcCallback longValueRpcCallback) {

        DefaultLongValue defaultLongValue = DefaultLongValue.newBuilder().setLongValue(0L).build();

  service.getLongValue(controller, defaultLongValue, longValueRpcCallback);

        if (LOG.isDebugEnabled()) {
            LOG.debug("Long value from server:" + longValueRpcCallback.getLongValue());
        }

        return longValueRpcCallback.getLongValue();

    }

    public static void main(String[] args) {

        String host = "localhost";
        int port = 9090;

        final String portArgKey = "--port=";
        for (String cmd : args) {
            if (cmd.startsWith(portArgKey)) {
                port = Integer.parseInt(cmd.substring(portArgKey.length()));
                break;
            }
        }

        TransportClient c = new TransportClient();
        c.open(host, port);
        c.getLongValue(new LongValueRpcCallback());
        c.close();
    }

    public TransportClient() {

    }

    public static class TransportModule {

  public static void contributeIoHandler(MappedConfiguration<String, ProtoService> configruation) {

   configruation.add(ProtoService.getDescriptor().getFullName(), new TransportHandler());
  }
 }
}
4

1 回答 1

0

由于回调,需要:

  1. 创建抽象类 LongValueRpcCallbackTemplate 实现 RpcCallback

  2. 创建类 LongValueRpcCallback 扩展 LongValueRpcCallbackTemplate

然后在测试类中完成实现

测试类:

package com.argmaps.client.proto;

import com.argmaps.transport.proto.generated.TransportServer;
import com.fepss.rpc.client.RpcChannelImpl;
import com.google.protobuf.RpcController;
import org.jmock.Expectations;
import org.junit.Test;
import org.junit.Before;
import org.junit.runner.RunWith;

import org.jmock.Mockery;
import org.jmock.integration.junit4.JUnit4Mockery;

import static org.junit.Assert.assertEquals;

public class TransportClientTest {

    Mockery context;

    @Before
    public void before() {

        context = new JUnit4Mockery();

    }

    private class TestLongValueRpcCallback extends LongValueRpcCallbackTemplate {

        private long longValue = 123456789L;

        @Override
        protected long getLongValue() {

            return longValue;
        }
    }


    @Test
    public void testGetLongValue() {

        final TransportServer.ProtoService.Interface mockedTransportServer = context.mock(TransportServer.ProtoService.Interface.class);

        final RpcChannelImpl channel = new RpcChannelImpl("localhost", 9090);
        final RpcController controller = channel.newRpcController();
        final TransportServer.DefaultLongValue defaultLongValue = TransportServer.DefaultLongValue.newBuilder().setLongValue(0L).build();

        com.argmaps.client.proto.TransportClient testObject = new TransportClient(controller, mockedTransportServer);

        final TestLongValueRpcCallback testLongValueRpcCallback = new TestLongValueRpcCallback();

        final long testLongValue = 123456789L;

        context.checking(new Expectations() {
            {
                one(mockedTransportServer).getLongValue(controller, defaultLongValue, testLongValueRpcCallback);
            }
        });

        assertEquals(testLongValue, testObject.getLongValue(testLongValueRpcCallback));

    }
}
于 2010-09-09T21:11:03.157 回答