gpt4 book ai didi

java - 谷歌 Protobuf 和 Jmock

转载 作者:行者123 更新时间:2023-12-01 05:54:16 25 4
gpt4 key购买 nike

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

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

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

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

最好、最干净的方法是什么

非常感谢!

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());
}
}
}

最佳答案

由于回调,需要:

  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));

}
}

关于java - 谷歌 Protobuf 和 Jmock,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3603458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com