title: How to Mock HikariDataSource and DataSourceProperties?
body: <p>How can I mock HikariDataSource and DataSourceProperties using springboot and Junit5 and Mockito?
  I found one possible solution but it seems to be rather very lengthy approach to test a single method, you can check <a href="https://stackoverflow.com/questions/59442210/how-to-mock-bean-hikaridatasource-correctly">here</a>
  I am trying to unit test one of the method 'ping' in my class but getting exception:</p>
  <pre><code>java.lang.NullPointerException
      at com.my.app.configuration.DbConfigurationTest.shouldReturnPing(DbConfgiurationTest.java:37)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:532)
      at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:115)
      at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:171)
      at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
      
  </code></pre>
  <p>Test Class:</p>
  <pre><code>import com.my.app.config.DbConfiguration;
  import org.junit.jupiter.api.Test;
  import org.mockito.InjectMocks;
  import org.mockito.Mockito;
  import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
  import org.springframework.boot.test.context.TestConfiguration;
  
  import static org.hamcrest.MatcherAssert.assertThat;
  import static org.hamcrest.Matchers.equalTo;
  import static org.hamcrest.Matchers.is;
  import static org.mockito.Mockito.when;
  
  @TestConfiguration
  class DbConfigurationTest
  {
   @InjectMocks static DbConfiguration dbConfiguration;
  
  
       DataSourceProperties dataSourceProperties= Mockito.mock(DataSourceProperties.class);
  
  
      @Test
      public void shouldReturnPing()
      {
          // expect
          boolean expectedResult = true;
          when(dbConfiguration.dataSourceProperties()).thenReturn(dataSourceProperties);
  when(dbConfiguration.dataSource(dataSourceProperties).isRunning()).thenReturn(true);
          boolean actualResult = dbConfiguration.ping();
  
          assertThat(actualResult, is(equalTo(expectedResult)));
      }
  
  
  }
  </code></pre>
  <p>Method class which needs to be tested:</p>
  <pre><code>import com.zaxxer.hikari.HikariDataSource;
  import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
  import org.springframework.boot.context.properties.ConfigurationProperties;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.context.annotation.Primary;
  
  /**
   * Database configuration.  Only properties override is required, everything else comes from spring boot auto-config.
   */
  @Configuration
  public class DbConfiguration
  {
  
      /**
       * Overrides the auto-configured bean in order to specify a different prefix for the DataSourceProperties
       *
       * @return A DataSourceProperties bean
       */
      @Bean
      @Primary
      @ConfigurationProperties(&quot;my.datasource&quot;)
      public DataSourceProperties dataSourceProperties()
      {
          return new DataSourceProperties();
      }
  
      /**
       * Overrides the {@link javax.sql.DataSource} bean creation from
       * {@link org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration}
       * to specify a different prefix for the {@link org.springframework.boot.context.properties.ConfigurationProperties}
       *
       * @param dataSourceProperties The properties to configure the datasource
       * @return A {@link com.zaxxer.hikari.HikariDataSource} instance
       */
      @Bean
      @ConfigurationProperties(&quot;my.datasource.configuration&quot;)
      public HikariDataSource dataSource(final DataSourceProperties dataSourceProperties)
      {
          return dataSourceProperties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
      }
  
  
      /**
       * Ping the service.
       *
       * @return the boolean
       */
      public boolean ping()
      {
          return dataSource(dataSourceProperties()).isRunning();
      }
  }
  </code></pre>
  <p>How can I successfully test the 'ping' method? I tried to create mock but failed.
  Thanks in advance :)</p>