title: Spring Test DBUnit H2Connection Refused
body: <p>I would like to create an integration test for my project and I am new to Spring Test DB Unit.  Unfortunately upon running the test I am encountering an error given below:</p>
  
  <p><a href="https://i.stack.imgur.com/AqFg3.png" rel="nofollow noreferrer">Error: Connection refused: Connect localhost</a></p>
  
  <p>I also added the the Base Test Config Class file where the configurations as well as how to connect to H2 database.</p>
  
  <pre><code>//import classes
  
      @Configuration
      @ComponentScan( {"com.projects.person", "com.projects.commons"} )
      @EnableWebMvc
      @EnableTransactionManagement
      @PropertySource( "classpath:application.properties" )
      public class BaseTestConfig extends WebMvcConfigurerAdapter
      {
  
          @Autowired
          private Environment env;
  
          @Autowired
          private OperatingSystem operatingSystem;
  
          @Autowired
          private PropertiesFactory propertiesFactory;
  
          @Bean
          public UrlBasedViewResolver setupViewResolver()
          {
              UrlBasedViewResolver resolver = new UrlBasedViewResolver();
              resolver.setPrefix( "/WEB-INF/pages/" );
              resolver.setSuffix( ".jsp" );
              resolver.setViewClass( JstlView.class );
              return resolver;
          }
  
          @Bean
          public LocalSessionFactoryBean sessionFactory() {
              LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
              sessionFactory.setDataSource(dataSource());
              sessionFactory.setPackagesToScan( "com.projects.person.model.db" );
              sessionFactory.setHibernateProperties(hibernateProperties());
              return sessionFactory;
          }
  
          @Bean
          public DataSource dataSource() {
  
              String databaseUrl = "jdbc:h2:tcp://localhost/persondb";
              if( operatingSystem.isWindows() )
              {
                  databaseUrl = "jdbc:h2:tcp://localhost/~/persondb";
              }
  
              DriverManagerDataSource dataSource = new DriverManagerDataSource();
              dataSource.setDriverClassName("org.h2.Driver");
              dataSource.setUrl( databaseUrl );
              dataSource.setUsername("sa");
              dataSource.setPassword("");
              return dataSource;
          }
  
          @Bean
          public H2Connection dbUnitConnection() throws SQLException, DatabaseUnitException {
              return new H2Connection(dataSource().getConnection(), "Person");
          }
  
          private Properties hibernateProperties() {
              Properties properties = new Properties();
              properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
              properties.put("hibernate.show_sql", "false");
              return properties;
          }
  
          @Bean
          public HibernateTransactionManager transactionManager(SessionFactory s) {
              HibernateTransactionManager transactionManager = propertiesFactory.createTransactionManagerProperty();
              transactionManager.setSessionFactory(sessionFactory().getObject());
              return transactionManager;
          }
      }
  </code></pre>