私は春のブートバッチ・アプリケーションをテストしています。私は私が持ついくつかのデータが、その失敗を初期化したいとメモリH2 DBで使用していますUnique index or primary key violation: PRIMARY_KEY_7 ON ...
。
私は微調整しようとしているapplication.properties
の追加と削除します、
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.properties.hibernate.default_schema=AD1
また、微調整spring.datasource.url
。
次のようにエンティティは次のとおりです。
@Table(schema = "ad1",name = "Student")
public class Student {
@Id
@Column(name = "student_id")
private String studentId;
@Column(name = "student_name")
private String studentName;
@Column(name = "favourite_subject")
private String favouriteSubject;
@Column(name = "enrollment_date")
private LocalDate enrollmentDate;
}
注意することはということであるschema
属性に設定されていますad1
data-h2.sql
これに住んでいることはsrc/test/resources
、次のとおりです。
Insert into ad1.Student (student_id,student_name,favourite_subject,enrollment_date) values ('1151AB26','John','Math',to_date('01-JAN-19','DD-MON-RR'));
application.properties
また、中には、src/test/resources
次のとおりです。
spring.datasource.platform=h2
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MODE=Oracle;INIT=CREATE SCHEMA IF NOT EXISTS AD1
spring.datasource.username=sa
spring.datasource.password=sa
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.properties.hibernate.default_schema=AD1
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.batch.job.enabled=false
注意することは、それがありますspring.datasource.url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MODE=Oracle;INIT=CREATE SCHEMA IF NOT EXISTS AD1
。私はAD1にスキーマを初期化しなければなりませんでした
私はAAキュウリのテストクラスを持っているので、私はキュウリを使用しています:
@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources", plugin = { "pretty", "html:target/cucumber" })
public class CucumberTest {
}
最後になりましたが春のブートテストクラス
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = StudentBatchApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration
public abstract class SpringCucumberIntegrationTest {
何が起こるかというと、私がテストを起動したときに、私は次のことを得ることです:
Caused by: org.h2.jdbc.JdbcSQLException: Unique index or primary key violation: "PRIMARY_KEY_7 ON AD1.Student(student_id) VALUES ('1151AB26', 1)"; SQL statement:
Insert into AD1.Student (student_id,student_name,favourite_subject,enrollment_date) values ('1151AB26','John','Math',to_date('01-JAN-19','DD-MON-RR')) [23505-197]
私は、このトラブルシューティングを行うときは、初期化が起きている瞬間にブレークポイントを置くことによって、プロセスは二回同じラインを実行します:
( /* key:1 */ '1151AB26', 'John', 'Math', TIMESTAMP '2019-01-01 00:00:00')
( /* key:2 */ '1151AB26', 'John', 'Math', TIMESTAMP '2019-01-01 00:00:00')
右この後、それは例外をスローします。
私が述べたように、トラブルシューティングの過程で私は1つのラインの内側があることに気づいdata-h2.sql
何とか二回、ひいては走っているこのユニークなインデックス違反を与えていました。さらにトラブルシューティング、私は(。すなわち、それはデータのスクリプトをロードしていたプロセスのポイントまでさかのぼるdata-h2.sql
)、それは二回このスクリプトをロードしたことに気づきました。一度target\classes\data-h2.sql
とで一回target\test-classes\data-h2.sql
。このスクリプトファイルはにコピーされた理由を私は正直わかりませんtarget\classes\data-h2.sql
。
最後に、私は、これは日食の問題であることがわかりました。私は、コマンドラインを使用してテストを実行したときmvn clean test
。テストは完璧に走ったが、私は日食でテストを実行したときRun As -> JUnit Test
、私はこの問題が発生します。私は日食でアプリケーションをきれいたびに、いくつかの理由でProject -> Clean
、それから、すべてのリソースファイルをコピーsrc/main/resources
してsrc/test/resources
へtarget\classes
。私は何をしなければならなかったことは、私に次の行を追加した.classpath
ファイル。
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>