1

How to write a JUnit test class for the below class? While I am trying to write I was getting "Cannot locate declared field com.bs.proteo.microservices.channels.sessionmobileadapter.business.openam.resource.ConnPoolConfig.connPoolRouteSize" error.

public class ConnPoolConfig {

    @Bean(name = "openAmHttpClient")
    public HttpClient buildConnectionPool(
            @Value("${login.connectionpool.route.size:10}") int connPoolRouteSize,
            @Value("${login.connectionpool.size:10}") int connPoolSize,
            @Value("${login.connectionpool.ttl:60}") int connPoolTtl,
            @Value("${login.connectionpool.evictidle:30}") int connPoolEvictIdle,
            @Value("${connection.http.timeout.connection.request}:5000") int connRequestTimeout,
            @Value("${connection.http.timeout.connect:5000}") int connectTimeout,
            @Value("${connection.http.timeout.socket:30000}") int socketTimeout){

        RequestConfig config = RequestConfig.custom()
                .setConnectionRequestTimeout(connRequestTimeout)
                .setConnectTimeout(connectTimeout)
                .setSocketTimeout(socketTimeout)
                .build();

        final HttpClient httpClient = HttpClientBuilder.create()
                .disableCookieManagement()
                .setMaxConnPerRoute(connPoolRouteSize)
                .setMaxConnTotal(connPoolSize)
                .setConnectionTimeToLive(connPoolTtl, TimeUnit.SECONDS)
                .setDefaultRequestConfig(config)
                .evictIdleConnections(connPoolEvictIdle, TimeUnit.SECONDS)
                .build();

        return httpClient;
    }
}
djm.im
  • 3,295
  • 4
  • 30
  • 45

1 Answers1

0

You have different alternatives. In the example below I will use only few lines referred to HttpClientBuilder, but you can use the same mechanism everywhere in your code.

Using Power Mockito

You can use PowerMockito in order to mock the first static call.

@RunWith(PowerMockRunner.class)
@PrepareForTest({HttpClientBuilder.class})
public class MyJunit{
    
   @Test
   public void myTest() {
      HttpClientBuilder client = Mockito.mock(HttpClientBuilder.class);
      PowerMockito.when(HttpClientBuilder.create()).thenReturn(client);
      myclassUnderTest.buildConnectionPool(...);
      Mockito.verify(client).disableCookieManagement();

   }
}

PowerMockito has some disadvatages; for example:

  1. your tests will slow down due the overhead needed to override the static methods
  2. if you use JaCoCo in order to keep track of your code coverage, then it won't work anymore.

Refactor your code

The only difference here is to avoid to use PowerMockito and refactor your class under test in a test friendly manner. The point here is to wrap the static method calls with protected methods; then we will mock all of this methods just created in order to return a mock object useful to verify the settings time by time.

Let's add a method like this to your class under test


 protected HttpClientBuilder create(){
    return HttpCLientBuilder.create();
 }

Now, your test method will be something like this

 @Test
 public void myTest(){
    HttpClientBuilder client = Mockito.mock(HttpClientBuilder.class);
    Mockito.doReturn(client).when(myclassUnderTest).create();
    myClassUnderTest.buildConnectionPool(...);
    Mockito.verify(client).disableCookieManagement();
}

Renato
  • 2,077
  • 1
  • 11
  • 22
  • Nice answer, but necessary to mention that PowerMockito is not available for Junit5 - but fortunately plain old Mockito from version 3.4.0 has mocking static methods including for Junit5. https://stackoverflow.com/questions/52830441/junit5-mock-a-static-method – racraman Oct 16 '21 at 12:22