title: How to mock connection to DB
body: <p>I have simple rest controller test, and it works when postgresql run.  I try run test, but before I stop service postgresql, and then my test failed. How I can mock connectin to database? I don't use DB in my test.</p>
  <pre><code> @SpringBootTest 
      @AutoConfigureMockMvc
      public class UserControllerTest {
          @Autowired
          private MockMvc mockMvc;
          @MockBean
          private UserService service;
          @WithMockUser(username = &quot;TestUser&quot;, roles = {&quot;SUPER_ADMIN&quot;})
          @Test
          public void testGetUserWhenUserIsAuthorized() throws Exception {
              UserAllInfo userAllInfo = new UserAllInfo(1L, 1L, &quot;sdfsdf@gmail.com&quot;, &quot;USER&quot;,
                      &quot;userenko&quot;, Collections.singletonList(1));
              doReturn(Optional.of(userAllInfo)).when(service).getUser(1);
              mockMvc.perform(get(&quot;/api/identity/user/{id}&quot;, 1L))
                      .andDo(print())
                      .andExpect(status().isOk())
                      .andExpect(jsonPath(&quot;$.userId&quot;, is(1)))
                      .andExpect(jsonPath(&quot;$.vendingBranchId&quot;, is(1)))
                      .andExpect(jsonPath(&quot;$.email&quot;, is(&quot;sdfsdf@gmail.com&quot;)))
                      .andExpect(jsonPath(&quot;$.name&quot;, is(&quot;USER&quot;)))
                      .andExpect(jsonPath(&quot;$.surname&quot;, is(&quot;userenko&quot;)))
                      .andExpect(jsonPath(&quot;$.roles&quot;, is(Collections.singletonList(1))));
          }
    @Test
      public void testGetUserWhenUserIsUnauthorized() throws Exception {
          mockMvc.perform(get(&quot;/api/identity/user/{id}&quot;, 1L))
                  .andDo(print())
                  .andExpect(status().isUnauthorized());
      }
      }
  </code></pre>
  <p>And I have list of exceptions. I think it's two solutions first create database in memory, second mock connection to database when i don't use it, but i don't know how I can do it.</p>
  <pre><code>18:00:57.542 INFO  [    task-1] org.hibernate.Version                    - HHH000412: Hibernate Core {5.2.17.Final}
  18:00:57.544 INFO  [    task-1] org.hibernate.cfg.Environment            - HHH000206: hibernate.properties not found
  18:00:57.609 INFO  [    task-1] o.h.annotations.common.Version           - HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
  18:00:57.757 WARN  [      main] o.s.b.a.o.j.JpaBaseConfiguration$JpaWebConfiguration - spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
  18:00:57.771 INFO  [    task-1] com.zaxxer.hikari.HikariDataSource       - HikariPool-1 - Starting...
  18:00:58.739 INFO  [      main] c.e.identity.rabbit.FanoutSender         - Sending startup notification...
  18:00:58.741 INFO  [      main] o.s.a.r.c.CachingConnectionFactory       - Attempting to connect to: [168.119.176.120:30672]
  18:00:58.817 ERROR [    task-1] com.zaxxer.hikari.pool.HikariPool        - HikariPool-1 - Exception during pool initialization.
  org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
      at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:303)
      at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:51)
      at org.postgresql.jdbc.PgConnection.&lt;init&gt;(PgConnection.java:225)
      at org.postgresql.Driver.makeConnection(Driver.java:465)
      at org.postgresql.Driver.connect(Driver.java:264)
      at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138)
      at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:358)
      at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206)
      at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:477)
      at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:560)
      at com.zaxxer.hikari.pool.HikariPool.&lt;init&gt;(HikariPool.java:115)
      at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112)
      at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122)
      at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:180)
      at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:68)
      at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
      at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)
      at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:259)
      at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:233)
      at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
      at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)
      at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
      at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:242)
      at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
      at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352)
      at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111)
      at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:861)
      at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:888)
      at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58)
      at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365)
      at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:391)
      at java.util.concurrent.FutureTask.run(FutureTask.java:266)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
      at java.lang.Thread.run(Thread.java:748)
  Caused by: java.net.ConnectException: Connection refused (Connection refused)
      at java.net.PlainSocketImpl.socketConnect(Native Method)
      at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
      at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
      at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
      at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
      at java.net.Socket.connect(Socket.java:607)
      at org.postgresql.core.PGStream.createSocket(PGStream.java:231)
      at org.postgresql.core.PGStream.&lt;init&gt;(PGStream.java:95)
      at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:98)
      at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:213)
      ... 34 common frames omitted
  18:00:58.818 WARN  [    task-1] o.h.e.j.e.i.JdbcEnvironmentInitiator     - HHH000342: Could not obtain connection to query metadata : Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
  18:00:58.836 INFO  [    task-1] org.hibernate.dialect.Dialect            - HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
  18:00:58.858 INFO  [    task-1] o.h.e.j.e.i.LobCreatorBuilderImpl        - HHH000422: Disabling contextual LOB creation as connection was null
  18:00:58.861 INFO  [    task-1] org.hibernate.type.BasicTypeRegistry     - HHH000270: Type registration [java.util.UUID] overrides previous : org.hibernate.type.UUIDBinaryType@569533c9
  18:00:59.036 INFO  [      main] o.s.a.r.c.CachingConnectionFactory       - Created new connection: rabbitConnectionFactory#33eb0d4:0/SimpleConnection@19cdc217 [delegate=amqp://rabbit@168.119.176.120:30672/, localPort= 44962]
  18:00:59.597 INFO  [    task-1] com.zaxxer.hikari.HikariDataSource       - HikariPool-1 - Starting...
  18:01:00.599 ERROR [    task-1] com.zaxxer.hikari.pool.HikariPool        - HikariPool-1 - Exception during pool initialization.
  org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
      at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:303)
      at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:51)
      at org.postgresql.jdbc.PgConnection.&lt;init&gt;(PgConnection.java:225)
      at org.postgresql.Driver.makeConnection(Driver.java:465)
      at org.postgresql.Driver.connect(Driver.java:264)
      at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138)
      at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:358)
      at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206)
      at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:477)
      at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:560)
      at com.zaxxer.hikari.pool.HikariPool.&lt;init&gt;(HikariPool.java:115)
      at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112)
      at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122)
      at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:180)
      at org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl.getIsolatedConnection(DdlTransactionIsolatorNonJtaImpl.java:43)
      at org.hibernate.tool.schema.internal.exec.ImprovedExtractionContextImpl.getJdbcConnection(ImprovedExtractionContextImpl.java:60)
      at org.hibernate.tool.schema.extract.internal.SequenceInformationExtractorLegacyImpl.extractMetadata(SequenceInformationExtractorLegacyImpl.java:40)
      at org.hibernate.tool.schema.extract.internal.DatabaseInformationImpl.initializeSequences(DatabaseInformationImpl.java:65)
      at org.hibernate.tool.schema.extract.internal.DatabaseInformationImpl.&lt;init&gt;(DatabaseInformationImpl.java:59)
      at org.hibernate.tool.schema.internal.Helper.buildDatabaseInformation(Helper.java:132)
      at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.doMigration(AbstractSchemaMigrator.java:96)
      at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:183)
      at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:72)
      at org.hibernate.internal.SessionFactoryImpl.&lt;init&gt;(SessionFactoryImpl.java:312)
      at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:462)
      at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:892)
      at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58)
      at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365)
      at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:391)
      at java.util.concurrent.FutureTask.run(FutureTask.java:266)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
      at java.lang.Thread.run(Thread.java:748)
  Caused by: java.net.ConnectException: Connection refused (Connection refused)
      at java.net.PlainSocketImpl.socketConnect(Native Method)
      at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
      at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
      at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
      at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
      at java.net.Socket.connect(Socket.java:607)
      at org.postgresql.core.PGStream.createSocket(PGStream.java:231)
      at org.postgresql.core.PGStream.&lt;init&gt;(PGStream.java:95)
      at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:98)
      at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:213)
      ... 32 common frames omitted
  18:01:00.602 WARN  [    task-1] o.h.e.jdbc.spi.SqlExceptionHelper        - SQL Error: 0, SQLState: 08001
  18:01:00.602 ERROR [    task-1] o.h.e.jdbc.spi.SqlExceptionHelper        - Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
  18:01:00.609 WARN  [      main] o.s.w.c.s.GenericWebApplicationContext   - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'fanoutReceiverFromVendingDeviceService' defined in file [/home/dev/IdeaProjects/dms-identity-service/target/classes/com/ecosoft/identity/rabbit/FanoutReceiverFromVendingDeviceService.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fanoutSender': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository' defined in com.ecosoft.identity.repository.UserRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Cannot resolve reference to bean 'jpaMappingContext' while setting bean property 'mappingContext'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
  18:01:00.826 INFO  [      main] o.s.o.j.LocalContainerEntityManagerFactoryBean - Closing JPA EntityManagerFactory for persistence unit 'default'
  18:01:00.826 WARN  [      main] o.s.b.f.s.DisposableBeanAdapter          - Invocation of destroy method failed on bean with name 'entityManagerFactory': javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
  </code></pre>
  <p>Update</p>
  <pre><code>@AutoConfigureMockMvc
  @WebMvcTest(UserController.class)
  @ComponentScan(&quot;com.ecosoft.identity&quot;)
  class UserController1Test {
      private String USER_NOT_EXISTS=&quot;User not exist&quot;;
      @Autowired
      private MockMvc mockMvc;
      @MockBean
      private UserService service;
      @MockBean
      private UserDetailsServiceImpl userDetailsService;
      @MockBean
      private AuthEntryPointJwt authEntryPointJwt;
      @MockBean
      private VendingBranchRepository vendingBranchRepository;
      @MockBean
      private FanoutSender fanoutSender;
  
      @WithMockUser(username = &quot;TestUser&quot;, roles = {&quot;SUPER_ADMIN&quot;})
      @Test
      public void testGetUserWhenUserIsAuthorized() throws Exception {
          UserAllInfo userAllInfo = new UserAllInfo(1L, 1L, &quot;sdfsdf@gmail.com&quot;, &quot;USER&quot;,
                  &quot;userenko&quot;, Collections.singletonList(1));
          doReturn(userAllInfo).when(service).getUser(1);
          mockMvc.perform(get(&quot;/api/identity/user/{id}&quot;, 1L))
                  .andDo(print())
                  .andExpect(status().isOk())
                  .andExpect(jsonPath(&quot;$.userId&quot;, is(1)))
                  .andExpect(jsonPath(&quot;$.vendingBranchId&quot;, is(1)))
                  .andExpect(jsonPath(&quot;$.email&quot;, is(&quot;sdfsdf@gmail.com&quot;)))
                  .andExpect(jsonPath(&quot;$.name&quot;, is(&quot;USER&quot;)))
                  .andExpect(jsonPath(&quot;$.surname&quot;, is(&quot;userenko&quot;)))
                  .andExpect(jsonPath(&quot;$.roles&quot;, is(Collections.singletonList(1))));
      }
  
  
  @Test
      public void testGetUserWhenUserIsUnauthorized() throws Exception {
          mockMvc.perform(get(&quot;/api/identity/user/{id}&quot;, 1L))
                  .andDo(print())
                  .andExpect(status().isUnauthorized());
      }
      }
  </code></pre>
  <p>But my test testGetUserWhenUserIsUnauthorized don't work, i get status 200, but I need get 401</p>