0

我想为我的项目创建一个集成测试,并且我是 Spring Test DB Unit 的新手。不幸的是,在运行测试时,我遇到了以下错误:

错误:连接被拒绝:连接本地主机

我还添加了 Base Test Config Class 文件,其中配置以及如何连接到 H2 数据库。

//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;
        }
    }
4

1 回答 1

0

连接被拒绝,因为databaseUrl您指定的服务器需要一个已经在运行并在侦听 TCP 端口的服务器localhost

所以你有两个选择。

  1. 确保您在localhost.
  2. 请改用嵌入式 H2 数据库,如下例所示。
@Bean
public DataSource dataSource() {
    return new EmbeddedDatabaseBuilder()
        .generateUniqueName(true)
        .setType(H2)
        .build();
}
于 2017-09-26T11:55:35.710 回答