title: How to mock params request unit test google cloud function?
body: <p>I was trying to implement was an unit test for my GCP function. And I want to mock the request param to test my function how do I do it?</p>
  <p>This is my GCP function:</p>
  <pre class="lang-java prettyprint-override"><code> @Override
      public void service(HttpRequest request, HttpResponse response)
          throws IOException {
          String name = request.getFirstQueryParameter(&quot;name&quot;).orElse(&quot;world&quot;);
  
          try {
              JsonElement requestParsed = gson.fromJson(request.getReader(), JsonElement.class);
              JsonObject requestJson = null;
  
              if (requestParsed != null &amp;&amp; requestParsed.isJsonObject()) {
                  requestJson = requestParsed.getAsJsonObject();
              }
  
              if (requestJson != null &amp;&amp; requestJson.has(&quot;name&quot;)) {
                  name = requestJson.get(&quot;name&quot;).getAsString();
              }
          } catch (JsonParseException e) {
              logger.severe(&quot;Error parsing JSON: &quot; + e.getMessage());
          }
  
          var writer = new PrintWriter(response.getWriter());
          writer.printf(&quot;Hello %s!&quot;, name);
      }
  </code></pre>
  <p>And this is my test:</p>
  <pre class="lang-java prettyprint-override"><code>public class HelloHttpTest {
  
      @Mock private HttpRequest request;
      @Mock private HttpResponse response;
  
      private BufferedWriter writerOut;
      private StringWriter responseOut;
      private static final Gson gson = new Gson();
  
      @BeforeEach
      public void beforeTest() throws IOException {
          MockitoAnnotations.initMocks(this);
  
          BufferedReader reader = new BufferedReader(new StringReader(&quot;&quot;));
          when(request.getReader()).thenReturn(reader);
  
          responseOut = new StringWriter();
  
          writerOut = new BufferedWriter(responseOut);
          when(response.getWriter()).thenReturn(writerOut);
      }
  
      @Test
      public void helloHttp_noParamsGet() throws IOException {
          new HelloHttp().service(request, response);
          writerOut.flush();
          Assertions.assertEquals(responseOut.toString(),&quot;Hello world!&quot;);
      }
  }
  </code></pre>
  <p>I want to test this line <code>name = requestJson.get(&quot;name&quot;).getAsString();</code> is working or not. How can I make a fake request with <code>name</code> params and the my GCP will get it?</p>