当前位置: 首页 > 知识库问答 >
问题:

如何在Spring boot中集成测试存储库

徐柏
2023-03-14

我是新的弹簧靴,做了我的第一个实体。如何测试存储库?我使用的是本地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/main/resources/application.properties中的我的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.

共有1个答案

白通
2023-03-14
@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace = NONE)
public class ClusterTests {

@Autowired
    private TestEntityManager entityManager;

    @Autowired
    private ClusterRepository clusterRepository;
}

这将有助于摆脱内存中的数据库生成。最好的方法是使用内存中的数据库,将实体持久化到该数据库中,但另一个好方法是直接使用数据库。通过使用@AutoConfigureTestDatabase(replace=NONE),您可以告诉spring不要替换数据源。

希望有帮助

 类似资料:
  • 我有一个服务,它有一个存储库作为构造函数的参数。 是默认的Spring存储库,没有实现 我在上编写,并使用编写测试。。保留的变体是使用方面,但没有完全像我希望的那样。会非常感激你的帮助。 更新:的用法: 配置:

  • 与@mockbean和@spybean一样,有没有类似于@fakebean/@dummybean的东西? 其思想是,该实例是100%真实的(具有预期的生产内部状态),并且它覆盖(或者添加bean,以防在配置中没有声明)上下文中的bean。理想情况下,您不需要创建TestConfiguration类并将其设置为Primary,因为这样可以在每个测试的基础上控制假冒,只有在需要时才可以。否则它使用主的

  • 我想用liquibase变更集进行模拟数据的集成测试,如何使其不影响真实数据库?我从这里找到了部分想法,但我使用的是springboot,我希望有更简单的解决方案。

  • 问题内容: 我有一个Java方法,可在Mongo集合的两个字段上创建索引。我应该获取集合的索引信息,然后检查索引的名称和字段是否正确。为此编写集成测试的最干净方法是什么?使用自定义的Hamcrest匹配器查看索引是否在集合中是否有意义? 问题答案: 在春天 使用,您可以获取的列表,代表MongoDB集合的索引。由于这是一个常规列表,因此您可以结合使用和进行断言: 如果您觉得这太难以理解或不方便使用

  • 我正在使用一个带有spring boot 2.0.0.rc1的多项目分级器。我的子项目之一是SpringBoot应用程序,其中包含了我的集成测试。 集成测试用WebEnvironment.random_port标记为@springboottest。由于未解析的依赖关系(在另一个子项目中声明的服务,的同级),测试失败,使用了gradle命令行,但在Eclipse IDE中成功。 如果有人有主意?如何