我有一个小项目,不包含运行 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;
知道我必须做什么吗?
这对我来说适用于 Spring Boot 2.0.3、H2 和最新的 RELEASE testng:
@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
Using @ContextConfiguration
with @DataJpaTest
删除该警告并且 IntelliJ 不会标记模拟taskRepository
as 无法自动装配。未找到“TaskRepository”类型的 bean。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)