- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
需要设置 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());
}
}
}
最佳答案
由于回调,需要:
创建抽象类LongValueRpcCallbackTemplate实现RpcCallback
创建类 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/
jar jmock-2.5.1.jar 和 jmock-junit3-2.5.1.jar 有什么区别? jmock-junit 包含这两个框架吗? 谢谢 最佳答案 从jmock-junit3-2.5.
我正在尝试完成我的第一个 JMOCK 教程 http://www.jmock.org/getting-started.html ,而且进展并不顺利。 我遇到的问题如下: java.lang.Secur
我是 jMock 的新手,所以我正在一个简单的例子中尝试一下。但我不明白为什么它不起作用。这是我正在测试的类: package com.application; import com.domain.C
我有一些功能想要模拟从 main 调用(静态:我也读过 - jmock mocking a static method )。我最近读到 JMock 不支持静态函数的模拟。好吧,相关的代码(这给我带来了
在 JUNIT 测试中,我使用 JMOCK。在一些示例中,我看到了类似于以下代码片段的代码: mock.checking(new Expectations(){
阅读我看到的 jmock 文档: First we must import the jMock classes, define our test fixture class and create a
我正在使用junt和jock。假设我有一个对象接口(interface) Contact,并且在我的测试类中有一个类似这样的方法: @Test public void testAddOneContac
我正在使用 iBatis DAO 框架为应用程序编写测试。该应用程序在 java 1.4 环境中运行,因此我使用所有旧版本(JDK 1.4、JUnit3、iBatis 2.3 和 JMock 1.2)
是否可以为模拟对象赋值。例如: myMockObject = context.mock(MyObject.class); myMockObject.setId("someId"); 我正在测试的方法结
我正在为一个需要在其内部创建多个集合的类编写 JMock 测试。我为该类提供一个工厂,它将在需要时生成一个集合。 interface Factory { Collection newColl
我对 JUnit 测试类有以下期望: CustomEvent myCustomEvent = new CustomEvent(data1, data2); m_context.checking(new
我目前正在尝试做一个垄断游戏的模拟测试课。 我们已经得到了一些关于如何设置其中一些的说明,但也许我们只是误解了 JMock 的工作原理。 我们有使用 takeTurn() 的 Player 类。我们有
我要测试的方法是用不同的参数调用模拟方法: public void methodToTest(){ getMock().doSomething(1); getMock().doSometh
我正在努力学习如何编写测试。我也在学习 Java,有人告诉我应该学习/使用/练习 jMock,我在网上找到了一些有助于某些扩展的文章,例如: http://www.theserverside.com/
我目前正在编写一些涉及 JMock 的测试。我无法理解以下代码结构: context.checking(new Expectations() { //context is of type Mocke
下面我只是想模拟一个名为 TestWrapper 的类并在其上设置“允许”期望。但是,在设定期望时我会出错。当使用 easymock 并且只是设置期望时,这似乎并没有发生 import org.jmo
我正在设置一个模拟对象,每次调用方法 f() 时它应该返回一个新的业务对象。如果我简单地说 returnValue(new BusinessObj()),它将在每次调用时返回相同的引用。如果我不知道
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: jmock mocking a static method 我正在研究使用 XX 类静态方法的遗留单元测试。
我正在使用 JMock 来测试使用对象的类的行为。我想测试 a() 方法是否被调用。但是,b() 和 c() 也会在该对象上调用。因此,如果我期望 a(),它也必须期望 b() 和 c() 才能使测试
我的代码中有一个静态方法,我想以某种方式对其进行模拟。 我正在使用 jmock。 我想我可以做到这一点的一种方法是在静态方法周围有一个“包装类”,并且 mock 这个,但我希望有一个更好的解决方案。
我是一名优秀的程序员,十分优秀!