2017-12-01 37 views
3

私はh2インメモリデータベースをテスト目的でのみ使用します。デフォルトのポートは8082と思われます。これは私のテストがヒロクで失敗する原因になります。組み込みh2データベース用のポート番号を設定

このポート番号を変更します。どうやってやるの ?

  1. は、このポートを指定し、私のローカルマシン($USER_HOME/.h2.server.properties)上のファイルがあるようです:私はこれまで何をやったか

    。そのファイルをアプリケーションワークスペースの外に持っていることは非常に奇妙なことですが、私はそれを英雄でチェックすることはできません。 、

  2. 私はapplication-test.propertiesport(それは性質が、私は私のテストのために使用するファイル)、webPortを設定しようとしましたが、それは私が異なる組み合わせserver.port、 `spring.data.source.h2.webPort試してみました(動作しませんでした build.gradle等)

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 

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

JDBC-URLには、ソケットもポートも必要としない接続文字列を含めることができます。それはテスト目的のためにそれを完全にします。 jdbc-connection-urlの例を挙げてください。 –

+0

どのようにh2サーバーを起動していますか?他のアプリケーションからデータベースに接続する必要がない場合は、なぜtcpサーバを起動していますか?残りのデータベース関連設定は何ですか? – Oleg

+0

'8082'は、h2console webServerで使用されるポートです。スプリングブートはそれを使用しません。 Beanを起動するBeanを持っているか、どこかで 'Server.createWebServer'を使って自分で起動するBeanがあります。ポートを変更することはあなたを助けません、あなたはヒーロー上で1つのポートしか使用できません。 – Oleg

答えて

0

解決策が見つかりました。以前は、テスト用の基本クラスを作成しました。ここでH2サーバーを作成します。ポートは同じクラスに設定されていました。

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

しかし、ポートを変更しても問題は解決しません。私は、eclipseが各テストを別々に実行する方法とは反対に、herokuはすべてを一度実行するので、サーバーは破壊されず、2つのテストの間に生き残ります。したがって、各テストクラスの実行が完了したら、サーバーを終了する必要があります。

@AfterClass 
public static void tearDown() throws SQLException { 
    webServer.stop(); 
} 
+0

なぜテスト用のWebサーバーが必要ですか? – Oleg

+0

エンドツーエンドのテストです。 –

関連する問題