设置嵌入式h2数据库的端口号

时间:2017-12-01 06:43:04

标签: spring heroku h2

我仅将h2内存数据库用于测试目的。默认端口似乎是8082,这导致我的测试在heroku上失败。

我想更改此端口号。我怎样才能做到这一点 ?

到目前为止我做了什么:

  1. 我的本​​地计算机($USER_HOME/.h2.server.properties)上似乎有一个文件指定了此端口。更不用说将这个文件放在应用程序工作区之外是很奇怪的,我无法在heroku中检查它。

  2. 我尝试在webPort中设置portapplication-test.properties(这是我用于测试的属性文件),但它不起作用(我尝试了不同的组合) server.port,`spring.data.source.h2.webPort等。)

  3. application-test.properties

    spring.datasource.url=jdbc:h2:mem:tesdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
    spring.datasource.driverClassName=org.h2.Driver
    spring.datasource.username=sa
    spring.datasource.password=
    spring.datasource.testWhileIdle = true
    spring.datasource.validationQuery = SELECT 1
    

    的build.gradle

    buildscript {
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.8.RELEASE")
        }
    }
    
    apply plugin: 'java'
    
    sourceCompatibility = 1.8
    targetCompatibility = 1.8
    tasks.withType(JavaCompile) {
        options.encoding = 'UTF-8'
    }
    
    test{
    
        forkEvery = 1
        testLogging {
            showStandardStreams = true
        }
    }
    
    repositories {
         maven { url "http://repo.maven.apache.org/maven2" }
    }
    
    dependencies {
        compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version:'1.5.8.RELEASE'
        compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version:'1.5.8.RELEASE'
        compile group: 'mysql', name: 'mysql-connector-java', version:'5.1.44'
        compile group: 'org.javassist', name: 'javassist', version:'3.18.0-GA'
        compile group: 'org.springframework.boot', name: 'spring-boot-starter-security', version:'1.5.8.RELEASE'
        compile group: 'org.springframework.security.oauth', name: 'spring-security-oauth2', version:'2.0.14.RELEASE'
        compile group: 'org.springframework.boot', name: 'spring-boot-configuration-processor', version:'1.5.8.RELEASE'
        compile group: 'io.jsonwebtoken', name: 'jjwt', version:'0.7.0'
        compile group: 'org.springframework.security', name: 'spring-security-cas', version:'4.2.3.RELEASE'
        compile group: 'org.springframework.security', name: 'spring-security-acl', version:'4.2.3.RELEASE'
        compile group: 'bsf', name: 'bsf', version:'2.4.0'
        compile group: 'org.springframework.boot', name: 'spring-boot-starter-batch', version:'1.5.8.RELEASE'
        compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator', version:'1.5.8.RELEASE'
        compile group: 'org.springframework.security', name: 'spring-security-ldap', version:'4.2.3.RELEASE'
        compile group: 'javax.jdo', name: 'jdo-api', version:'3.0.1'
        compile group: 'com.querydsl', name: 'querydsl-core', version:'4.1.4'
        compile group: 'org.springframework', name: 'springloaded', version:'1.2.8.RELEASE'
        compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-rest', version:'1.5.8.RELEASE'
        compile group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib', version:'1.0.6'
        compile group: 'org.scala-lang', name: 'scala-library', version:'2.11.0'
        compile group: 'commons-validator', name: 'commons-validator', version:'1.6'
        compile group: 'commons-io', name: 'commons-io', version:'2.5'
        compile group: 'org.springframework.boot', name: 'spring-boot-starter-jersey', version:'1.5.8.RELEASE'
        compile group: 'org.modelmapper', name: 'modelmapper', version:'0.7.5'
        compile(group: 'org.apache.httpcomponents', name: 'httpclient', version:'4.5.3') {
    exclude(module: 'commons-logging')
        }
        compile group: 'org.apache.commons', name: 'commons-lang3', version:'3.4'
        compile(group: 'org.springframework.boot', name: 'spring-boot-starter-test', version:'1.5.8.RELEASE') {
    exclude(module: 'commons-logging')
        }
        compile group: 'org.eclipse.jetty.aggregate', name: 'jetty-all', version:'9.2.13.v20150730'
        compile group: 'com.h2database', name: 'h2', version:'1.4.196'
        compile group: 'org.springframework.integration', name: 'spring-integration-test', version:'4.3.12.RELEASE'
        compile group: 'org.springframework.security', name: 'spring-security-test', version:'4.2.3.RELEASE'
        compile group: 'org.apache.hadoop', name: 'hadoop-core', version:'1.0.0'
        compile group: 'com.sleepycat', name: 'je', version:'5.0.73'
        compile group: 'commons-dbcp', name: 'commons-dbcp', version:'1.4'
        compile group: 'org.springframework', name: 'spring-webmvc', version:'4.3.12.RELEASE'
        compile group: 'org.springframework.data', name: 'spring-data-commons', version:'1.13.8.RELEASE'
        compile group: 'org.springframework.hateoas', name: 'spring-hateoas', version:'0.23.0.RELEASE'
        compile group: 'org.springframework.data', name: 'spring-data-rest-core', version:'2.6.8.RELEASE'
        compile group: 'org.springframework.plugin', name: 'spring-plugin-core', version:'1.2.0.RELEASE'
        compile group: 'org.springframework', name: 'spring-orm', version:'4.3.12.RELEASE'
        testCompile group: 'junit', name: 'junit', version:'4.11'
        testCompile group: 'org.springframework', name: 'spring-test', version:'4.3.12.RELEASE'
    }
    

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。之前,我为我的测试创建了一个基类,在那里我创建了我的H2服务器。该端口设置在同一个类中。

@BeforeClass
public static void init() throws SQLException {
    webServer = Server.createWebServer("-web", "-webAllowOthers", "-webPort", "8082");
    webServer.start();
}

但改变端口并不能解决问题。我发现,与eclipse分别运行每个测试的方式相反,heroku运行所有测试一次,因此服务器不会被破坏并在两次测试之间保持活动状态。因此,我必须在每个测试类运行完毕后终止服务器:

@AfterClass
public static void tearDown() throws SQLException {
    webServer.stop();
}
相关问题