prasanthkumar2392 edited a comment on issue #6642: Context in Pulsar functions 
not getting mocked
URL: https://github.com/apache/pulsar/issues/6642#issuecomment-608241467
 
 
   Hi, 
   
   - I am using functions api 2.5.0 and I am using context to set the key and 
value inside functions and sending it to the output topic.
   
   - But in my test class, I am trying to mock the context and i am 
unsuccesfull.
   (Also tried context configuration of puslar test container to see if context 
is not null)
   
   My function class to process the messages 
   
   ```
   import org.apache.pulsar.client.api.PulsarClientException;
   import org.apache.pulsar.client.api.Schema;
   import org.apache.pulsar.functions.api.Context;
   import org.apache.pulsar.functions.api.Function;
   import org.slf4j.Logger;
   
   
   public class MyFunction implements Function<byte[], Void> {
   
       @Override
       public Void process(byte[] input, Context context) throws Exception {
           Logger log = context.getLogger();
           log.debug("Entered ");
           
           //Initialize Required classes to process message
   
           if (input != null) {
               try {
                   // assign the key and value and send it to output topic
                   log.debug("Key  :");
                   log.debug("Value ");
                   context.newOutputMessage(context.getOutputTopic(), 
Schema.STRING)
                           .key("")
                           .value("")
                           .send();
   
               } catch (PulsarClientException e) {
                   log.error("error in " + e.toString());
               }
           }
           return null;
       }
   
   }
   
   ```
   
   
   and my sample test class looks like this 
   ```
   import com.ericsson.iota.dm.function.MyFunction;
   import org.apache.pulsar.functions.api.Context;
   import org.junit.jupiter.api.Test;
   import static org.mockito.Mockito.mock;
   
   public class FunctionTest {
   
       @Test
       public void functionProcessing() throws Exception {
           String message ="{\n" +
                   "   \"name\": \"XXXXX\",\n" +
                   "   \"email\": \"[email protected]\"\n" +
                   "}";
           MyFunction myFunction =new MyFunction();
           myFunction.process(message.getBytes(), mock(Context.class));
           // Assert conditions
       }
   }
   
   ```
   
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to