【问题标题】:DAO Layer test: java.lang.IllegalStateException: Configuration error: found multiple declarations of @BootstrapWith for test classDAO 层测试:java.lang.IllegalStateException:配置错误:为测试类找到多个@BootstrapWith 声明
【发布时间】:2020-04-13 16:19:56
【问题描述】:

我尝试使用 Spring Boot 为我的 DAO 层运行测试。但我收到 java.lang.IllegalStateException: Configuration error: found multiple declarations of @BootstrapWith for test class [com.example.demo.FoodDaoIntegrationTest]

我已经阅读了几个关于这个错误的答案,但仍然没有帮助。我想注释仍然存在某种问题。 我的测试是:

package com.example.demo;
@RunWith(SpringRunner.class) @DataJpaTest
@SpringBootTest(classes = com.project.application.DemoApplication.class) 
@ContextConfiguration(classes = com.project.application.DemoApplication.class) 
public class FoodDaoIntegrationTest  {

@Autowired
private TestEntityManager entityManager;

@Autowired
private FoodDao mealDao;

@Test
public void TestSomething() {

【问题讨论】:

    标签: java spring-boot testing spring-data-jpa dao


    【解决方案1】:

    您的注释很混乱。 SpringBootTest 和 DataJpaTest 不能混用

    试试这个:

    @RunWith(SpringRunner.class) 
    @DataJpaTest
    public class FoodDaoIntegrationTest  {
    

    【讨论】:

    • 如果我尝试删除 SpringBootTest 和 ContextConfiguration,我会得到 .IllegalStateException: Unable to find a@SpringBootConfiguration,您需要在测试中使用 ContextConfiguration 或 SpringBootTest(classes=...)。跨度>
    • 如果我保留 ContextConfiguration 我会得到 UnsatisfiedDependencyException: Error created bean with name 'com.example.demo.FoodlDaoIntegrationTest': Unsatisfied dependency expressed through field 'mealDao';嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type..
    • 你的DemoApplication在包结构中的什么位置?您应该始终将 Application 类放在所有其他类之上的根包中
    猜你喜欢
    • 1970-01-01
    • 2019-10-16
    • 2017-04-24
    • 2013-01-24
    • 2020-11-01
    • 2020-09-22
    • 1970-01-01
    • 2020-08-06
    • 2018-02-11
    相关资源
    最近更新 更多