Home > Enterprise >  How to write a unit test for custom Rest template interceptor in spring boot
How to write a unit test for custom Rest template interceptor in spring boot

Time:03-18

I have this following custom interceptor in my java spring application as below.

public class AuthInterceptor implements ClientHttpRequestInterceptor {

    private final String encodedCredentials;

    public AuthInterceptor(String username, String password) {
        this(username, password, (Charset) null);
    }

    public AuthInterceptor(String username, String password, @Nullable Charset charset) {
        this.encodedCredentials = HttpHeaders.encodeBasicAuth(username, password, charset);
    }

    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
        HttpHeaders headers = request.getHeaders();
        headers.setBasicAuth(this.encodedCredentials);
        return execution.execute(request, body);
    }

}

But I am looking to find for some documentation on how to write a unit test for this class and could not find. Any input on how to test would be really helpful

CodePudding user response:

Unit test that class the same way you unit test any class.

  1. classToTest = new AuthInterceptor()
  2. Call the method with Mock objects.

Some example code:

@ExtendWith(MockitoExtension.class)
public class TestAuthInterceptor
{
    private AuthInterceptor classToTest;

    @Mock
    private ClientHttpRequestExecution mockClientHttpRequestExecution;
    
    @Mock
    private ClientHttpResponse mockClientHttpResponse;
    
    @Mock
    private HttpHeaders mockHttpHeaders;
    
    @Mock
    private HttpRequest mockHttpRequest;

    @BeforeEach
    public void beforeEach()
    {
        classToTest = new AuthInterceptor("username", "password");
    }
    
    @Test
    public void intercept_allGood_success()
    {
        final ClientHttpResponse actualResult;
        final byte[] inputBody = null;
        
        doReturn(mockHttpHeaders).when(mockHttpRequest).getHeaders();
        doReturn(mockClientHttpResponse).when(mockClientHttpRequestExecution).execute(mockHttpRequest, inputBody);


        actualResult = classToTest.intercept(mockHttpRequest, inputBody, mockClientHttpRequestExecution);
        

        assertSame(mockClientHttpResponse, actualResult);
        // other asserts as desired.
    }
}

CodePudding user response:

Assuming you only want to test the interception and you already have set up mockito:

 @Test
  @DisplayName("Should add correct header to authorization")
  void encodeProperly() throws Exception {
    AuthInterceptor cut  = new AuthInterceptor("someuserName","somePaswwordName");

    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/test");
    cut.intercept(request, null,null,mockedExecution );

    ArgumentCaptor<HttpRequest> argumentCapture = ArgumentCaptor.forClass(HttpRequest.class) ;
    verify(mockedExecution.execute(argumentCapture.capture(),any()));
    
    assertEquals(expectedEncodedCredentials,argumentCapture.getValue().getHeaders().get(HttpHeaders.AUTHORIZATION));
  }

so the steps are: create your class to test cut

then create a mock of MockHttpServletRequest it will be helpful to perform your check

  • Related