【问题标题】:Test Spring Data Repository in a project without a Spring Boot Application main class在没有 Spring Boot Application 主类的项目中测试 Spring Data Repository
【发布时间】:2018-10-18 06:19:34
【问题描述】:

我有一个不包含运行 Spring Boot 应用程序的类的小项目。在那个类中,我只有一些配置和一些存储库。我想在小项目中测试这些存储库。

为此,我有以下几点:

@SpringBootTest
@DataJpaTest
public class TaskRepositoryTest extends AbstractTestNGSpringContextTests {

    @Autowired
    private TaskRepository taskRepository;

    @Test
    public void test() {
        taskRepository.save(new Task("open"));
    }
}

但我收到以下错误

Caused by: java.lang.NoSuchMethodError: org.springframework.boot.jdbc.DataSourceBuilder.findType(Ljava/lang/ClassLoader;)Ljava/lang/Class;

知道我要做什么吗?

【问题讨论】:

  • 没有@DataJpaTest注解你累了吗?

标签: spring-boot spring-data-jpa testng


【解决方案1】:

这适用于 Spring Boot 2.0.3、H2 和最新的 RELEASE 测试:

@EnableAutoConfiguration
@ContextConfiguration(classes = TaskRepository.class)
@DataJpaTest
public class TaskTest extends AbstractTestNGSpringContextTests {

   @Autowired
   private TaskRepository taskRepository;

   @Test
   public void test() {
      taskRepository.save(new Task("open"));
   }

}

LE:

在以前版本的答案中,我使用了@SpringBootTest @DataJpaTest,但这似乎是错误的做法。 将出现以下消息:

[main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - 使用 SpringBootContextLoader 没有为测试类 [one.adf.springwithoutapp.TaskTest] 找到 @ContextConfiguration 和 @ContextHierarchy

@ContextConfiguration@DataJpaTest 一起使用会删除该警告,并且IntelliJ 不会将模拟的taskRepository 标记为无法自动装配。找不到“TaskRepository”类型的 bean。

【讨论】:

  • 我试过了,但我仍然有同样的异常:java.lang.IllegalArgumentException: Given type must be an interface!如果我从类本身开始测试,或者如果我从 Maven->Install in IntelliJ 开始,则出现此异常: 原因:java.lang.ClassNotFoundException: ch.qos.logback.classic.turbo.TurboFilter stackoverflow.com/questions/62313098/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-14
  • 2014-07-21
  • 2022-10-06
  • 2019-01-21
  • 2019-04-11
  • 2021-04-23
  • 2016-01-12
相关资源
最近更新 更多