You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2020/03/31 07:37:00 UTC

[GitHub] [pulsar] prasanthkumar2392 opened a new issue #6642: Context in Pulsar functions not getting mocked

prasanthkumar2392 opened a new issue #6642: Context in Pulsar functions not getting mocked
URL: https://github.com/apache/pulsar/issues/6642
 
 
   **I am trying to write a unit test for Pulsar functions, and if I try to Mock Context class, I get null pointer exception**
   
   
   **Steps to reproduce the behavior:**
   
   - I am creating a pulsar function which does some processing logic on the messages.
   
   -  I am using Context interface inside the function to get logger, get output topic and newOutputMessage.
   
   While writing unit tests for functions, I am mocking the context as per this example but I land up in facing null pointer exception as Context class is not getting mocked
   
   [http://pulsar.apache.org/docs/ja/2.4.0/functions-debugging/#use-unit-test](url)
   
   **Pulsar functions -2.5.0**
   
   

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [pulsar] jiazhai commented on issue #6642: Context in Pulsar functions not getting mocked

Posted by GitBox <gi...@apache.org>.
jiazhai commented on issue #6642: Context in Pulsar functions not getting mocked
URL: https://github.com/apache/pulsar/issues/6642#issuecomment-606993558
 
 
   @prasanthkumar2392 What is your test looks like? 

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [pulsar] jiazhai commented on issue #6642: Context in Pulsar functions not getting mocked

Posted by GitBox <gi...@apache.org>.
jiazhai commented on issue #6642: Context in Pulsar functions not getting mocked
URL: https://github.com/apache/pulsar/issues/6642#issuecomment-610718219
 
 
   @prasanthkumar2392  It looks more like a mock issue. looks like we need to mock the need classes and methods.

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [pulsar] prasanthkumar2392 edited a comment on issue #6642: Context in Pulsar functions not getting mocked

Posted by GitBox <gi...@apache.org>.
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\": \"xxxxx@xxx.com\"\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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [pulsar] prasanthkumar2392 commented on issue #6642: Context in Pulsar functions not getting mocked

Posted by GitBox <gi...@apache.org>.
prasanthkumar2392 commented 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 
   `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\": \"xxxxx@xxx.com\"\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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [pulsar] prasanthkumar2392 edited a comment on issue #6642: Context in Pulsar functions not getting mocked

Posted by GitBox <gi...@apache.org>.
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 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\": \"xxxxx@xxx.com\"\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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [pulsar] jiazhai edited a comment on issue #6642: Context in Pulsar functions not getting mocked

Posted by GitBox <gi...@apache.org>.
jiazhai edited a comment on issue #6642: Context in Pulsar functions not getting mocked
URL: https://github.com/apache/pulsar/issues/6642#issuecomment-606993558
 
 
   @prasanthkumar2392 What is your test looks like? 
   And it would be good to reference to 2.5.0 instead of 2.4.0 related docs
   http://pulsar.apache.org/docs/en/functions-debugging/

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services