私は最初のエンティティを作った、Spring Bootを初めて使う人です。リポジトリをテストするにはどうしたらいいですか? 私はローカルMySQLデータベースを使用しています。だから最初のステップは、データベースを嘲笑するだろうか?春のブートでテストエンティティをユニット化する方法
@RunWith(SpringRunner.class)
@DataJpaTest
public class ClusterTest {
@Autowired
private TestEntityManager testEntityManager;
@Autowired
private ClusterRepository clusterRepository;
@Test
public void testExample() throws Exception{
this.testEntityManager.persist(new Cluster("Project", "System"));
Cluster cluster = this.clusterRepository.findOne(1);
assertThat(testcluster.getProject()).isEqualTo("System");
}
}
これは私のクラスタエンティティ
@Entity
@Table(name = "Cluster")
public class Cluster {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String project;
public Cluster(){
}
public Cluster(String project) {
this.project = project;
}
ですそして、私のリポジトリはちょうどJpaRepositoryを拡張します。
私が取得しているエラーはjava.lang.IllegalStateExceptionです:ApplicationContextの読み込みに失敗しました。そして少し下:テスト用の組み込みデータベースでDataSourceを置き換えられませんでした。組み込みデータベースが必要な場合は、サポートされているデータベースをクラスパスに配置するか、@AutoconfigureTestDatabaseのreplace属性を調整してください。
これは、SRC /メイン/リソース/ application.propertiesから私のapplication.propertiesファイルです:私は私のsrc /テスト/リソース/ application.propertiesに
をこのファイルをコピーして
security.basic.enabled=false
spring.thymeleaf.cache=false
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
エラー
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:44)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Failed to replace DataSource with an embedded database for tests. If you want an embedded database please put a supported one on the classpath or tune the replace attribute of @AutoconfigureTestDatabase.
テストクラスでスプリング構成ファイルを指定していないため、エラーが発生しています。 –
完全な例が必要なのか、それともエラーだけの解決策が必要ですか? –
@VivekSingh可能であれば、例。私はこのすべてに非常に新しいです。だから、私が間違ったことを知り、それから学ぶ – Urban