title: How to write junit5 test for gateway cloud filter And how to I get full code coverage of this code
body: <p>I want to write junit test for my gateway cloud filter but problem is i am new on unit test. please help  me. and here is one private method for authorization.</p>
  <p>[If anyone not know how to write spring Gateway cloud Filter then please go through my gateway filter method. and logic ]</p>
  <pre><code>@Component
  @Slf4j
  public class AuthorizationFilter extends AbstractGatewayFilterFactory&lt;AuthorizationFilter.Config&gt; {
  
      @Value(&quot;${rest.auth.service.url}&quot;)
      private String authServiceUrl;
  
      @Autowired
      RestTemplate restTemplate;
  
      public AuthorizationFilter() {
          super(Config.class);
      }
  
      private Mono&lt;Void&gt; onError(ServerWebExchange exchange, String err, HttpStatus httpStatus) {
          ServerHttpResponse response = exchange.getResponse();
          response.setStatusCode(httpStatus);
  
          return response.setComplete();
      }
      //Here is my authorization details 
      private UserRoleDetailsDTO getAuthorizationDetails(String authorizationHeader) {
          try {
              log.error(&quot;Checking authorization&quot;);
              HttpEntity&lt;?&gt; entity = new HttpEntity(null, new HttpHeaders() {{
                  add(&quot;Authorization&quot;, authorizationHeader);
                  add(&quot;Accept&quot;, &quot;application/json&quot;);
              }});
  
              ResponseEntity&lt;Map&gt; responseMap = restTemplate.exchange(authServiceUrl.concat(&quot;/v1/profile&quot;), HttpMethod.GET, entity, Map.class);
              System.out.println(responseMap);
              if (responseMap.getBody() == null)
                  log.warn(&quot;Received null body in response. Request will end now&quot;);
              Map data = (Map) responseMap.getBody().get(&quot;data&quot;);
              boolean isAuthorized = data.get(&quot;authorized&quot;) == null || (boolean) data.get(&quot;authorized&quot;);
              UserRoleDetailsDTO userRoleDetailsDTO = UserRoleDetailsDTO.builder().userId(String.valueOf(data.get(&quot;userId&quot;))).
                      roles((List&lt;RoleDTO&gt;) data.get(&quot;roles&quot;)).isAuthorized(isAuthorized).build();
              return userRoleDetailsDTO;
          } catch (RestClientException e) {
              String errorResponse = null;
              if (e instanceof HttpClientErrorException) {
                  HttpClientErrorException exception = (HttpClientErrorException) e;
                  errorResponse = exception.getResponseBodyAsString();
              } else errorResponse = e.getMessage();
              log.error(&quot;error with http connect with error response : {} : {}&quot;, errorResponse, e);
              return null;
          } catch (Exception e) {
              log.error(&quot;error with http connect : {}&quot;, e);
              return null;
          }
      }
      // Here is my Gateway filter
      @Override
      public GatewayFilter apply(Config config) {
  
          return (exchange, chain) -&gt; {
              ServerHttpRequest request = exchange.getRequest();
  
              String requestId;
              if (request.getHeaders().containsKey(&quot;Request-Id&quot;)) {
                  requestId = request.getHeaders().get(&quot;Request-Id&quot;).get(0);
              } else {
                  requestId = UUID.randomUUID().toString();
                  log.debug(&quot;Request Id not passed. {} is genertaing requestId {}&quot;, AuthorizationFilter.class.getSimpleName(), requestId);
              }
              MDC.put(&quot;requestId&quot;, requestId);
              request.mutate().header(&quot;Request-Id&quot;, requestId);
  
              if (!request.getHeaders().containsKey(&quot;Authorization&quot;)) {
                  log.error(&quot;No authorization header&quot;);
                  return onError(exchange, &quot;No Authorization header&quot;, HttpStatus.UNAUTHORIZED);
              }
  
              String authorizationHeader = request.getHeaders().get(&quot;Authorization&quot;).get(0);
  
              UserRoleDetailsDTO userRoleDetailsDTO = getAuthorizationDetails(authorizationHeader);
              if (userRoleDetailsDTO == null || !userRoleDetailsDTO.isAuthorized()) {
                  log.error(&quot;Invalid authorization header&quot;);
                  return onError(exchange, &quot;Invalid Authorization header&quot;, HttpStatus.UNAUTHORIZED);
              }
              String userDetailsStr = &quot;&quot;;
              try {
                  userDetailsStr = new ObjectMapper().writeValueAsString(userRoleDetailsDTO);
                  System.out.println(&quot;value 1 :&quot;+userDetailsStr);
              } catch (JsonProcessingException e) {
                  log.error(&quot;Exception writing user details to string. &quot;, e);
              }
              if (StringUtils.isEmpty(userDetailsStr))
                  log.warn(&quot;Empty User Details string being passed!!&quot;);
              request.mutate().header(ApplicationConstants.USER_DETAILS_HEADER, userDetailsStr);
              System.out.println(&quot;value 2 :&quot;+request.mutate().header(ApplicationConstants.USER_DETAILS_HEADER, userDetailsStr));
              return chain.filter(exchange.mutate().request(request).build());
          };
  
      }
  
      @Override
      public Config newConfig() {
          return new Config();
      }
  
      public static class Config {
          // Put the configuration properties
      }
  
  }
  </code></pre>
  <p>here is my user role details</p>
  <pre><code>public class UserRoleDetailsDTO {
      private String userId;
      private List&lt;RoleDTO&gt; roles;
      private boolean isAuthorized = true;
  } 
  </code></pre>
  <p>and RoleDto class</p>
  <pre><code>  public class RoleDTO {
          private String name;
          private int weight;
      }
  </code></pre>
  <p>here is my junit test when I run with code coverage then I am not getting fully code coverage please help me</p>
  <pre><code> public YourClassTest{   
  
      @Test
      void apply() throws Exception {
          AuthorizationFilter authorizationFilter  = new AuthorizationFilter();
          AuthorizationFilter.Config config = new YourClass.Config();
          GatewayFilter filter =authorizationFilter.apply(config);
          String st ;
          st = new  ObjectMapper().writeValueAsString(userRoleDetailsDTO);
               MockServerHttpRequest expected  = MockServerHttpRequest.get(&quot;/v1/profile&quot;).
                 header(&quot;Authorization&quot;, &quot;Bearer sdfsf&quot;,st, TestConstants.USER_DETAILS_HEADER).build();
          MockServerWebExchange exchange = MockServerWebExchange.from(expected);
          filter.filter(exchange,FilterChain);
          String requestid;
          ServerHttpRequest actual =  exchange.getRequest();
          assertEquals(expected,actual);
      }
   }
  </code></pre>