2010-08-30 3 views
1

devez configurer le code JMock pour tester téléphonique avec Google ProtobufGoogle Protobuf et JMock

projet complet est situé à http://github.com/andrewmilkowski/template-transport

En bref, voici des méthodes signatures (ci-dessous)

ce que je dois à faire est la méthode d'essai getLongValue, en utilisant jMock JUnit4Mockery

ce qui est le meilleur et le plus propre d'aller sur ce

merci beaucoup!

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

Répondre

0

En raison de la fonction de rappel, nécessaire pour:

  1. créer LongValueRpcCallbackTemplate abstrait classe implémente RpcCallback

  2. créer la classe LongValueRpcCallback étend LongValueRpcCallbackTemplate

, puis la mise en œuvre complète en le test cl ass

classe Test:

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

    } 
} 
Questions connexes