Urb*_*ban 4 java mysql spring integration-testing
我是 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");
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的集群实体
@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;
}
Run Code Online (Sandbox Code Playgroud)
我的存储库只是扩展了 JpaRepository。
我收到的错误是 java.lang.IllegalStateException: 无法加载 ApplicationContext。还有一点不足:无法用嵌入式数据库替换 DataSource 进行测试。如果您想要一个嵌入式数据库,请将受支持的数据库放在类路径上或调整@AutoconfigureTestDatabase 的替换属性。
这是我的 application.properties 文件,来自 src/main/resources/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
Run Code Online (Sandbox Code Playgroud)
我将此文件复制到我的 src/test/resources/application.properties
和错误
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.
Run Code Online (Sandbox Code Playgroud)
@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace = NONE)
public class ClusterTests {
@Autowired
private TestEntityManager entityManager;
@Autowired
private ClusterRepository clusterRepository;
}
Run Code Online (Sandbox Code Playgroud)
这应该有助于摆脱内存数据库生成。最好的方法是使用内存数据库来保存实体,但另一种好的方法是直接使用数据库。通过使用 @AutoConfigureTestDatabase(replace = NONE) 你告诉 spring 不要替换数据源。
希望能帮助到你
| 归档时间: |
|
| 查看次数: |
7160 次 |
| 最近记录: |