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



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

我想更改此端口号。我该怎么做?

到目前为止我做了什么:

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

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

应用程序测试属性

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'
}

我找到了解决方案。早些时候,我为我的测试创建了一个基类,在那里我创建了我的 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();
}

相关内容

最新更新