简体   繁体   English

没有可用的“Package.TestDaoRepo”类型的合格 bean:预计至少有 1 个 bean 有资格作为自动装配候选

[英]No qualifying bean of type 'Package.TestDaoRepo' available: expected at least 1 bean which qualifies as autowire candidate

Hey I am using spring boot version 2.3.4 for my web application.嘿,我的 Web 应用程序使用的是 Spring Boot 2.3.4 版。 I am writing a JUNIT Integration test case for testing my controller, service and Repo using WebMvcTest.我正在编写一个 JUNIT 集成测试用例,用于使用 WebMvcTest 测试我的控制器、服务和 Repo。

@ExtendWith(SpringExtension.class)
@WebMvcTest(TestCotroller.class)
class ITTestController {

@Test
void test() {
    System.out.println("Test me ");
   // URL to call. controller and rest of the logic 
}

}

I am looking for minimum configuration to load so i have used @WebMvcTest.我正在寻找要加载的最低配置,因此我使用了 @WebMvcTest。

This is my controller need to be tested这是我的控制器需要测试

@RestController
@RequestMapping(value = APIUrlConstants.VERSION + APIUrlConstants.TEST_CONTROLLER)
public class TestCotroller {
    @Autowired
    TestServiceInf testServiceInf;
    
    @RequestMapping(value = APIUrlConstants.TEST_DB, method = RequestMethod.GET)
    public ResponseEntity<String> testDbStatus() {
        Long count = testServiceInf.testDbStatus();
        String responseMessage = "DB is up and running, total number of products count are --  " + count;
       return new ResponseEntity<String>(responseMessage, HttpStatus.OK);
        
    }
    
    @RequestMapping(value = APIUrlConstants.TEST_APPLICATION, method = RequestMethod.GET)
    public ResponseEntity<String> testApplicationStatus() {
        
        String responseMessage = testServiceInf.testApplication();
       return new ResponseEntity<String>(responseMessage, HttpStatus.OK);
        
    }
    
    }

The Service class服务类

  @Service
public class TestServiceImpls implements TestServiceInf {
    
    @Autowired
    TestDaoRepo testDaoRepo;
    
    private static String responseMessage = "Application is up and running";
    
    @Override
    public Long testDbStatus() {
        
        Long countProduct = testDaoRepo.count();
        System.out.println(countProduct);
        return countProduct;
    }


    @Override
    public String testApplication() {
        
        return responseMessage;
    }

}

Repo class回购类

@Repository
public interface TestDaoRepo extends JpaRepository<CpcMasterProduct, Long> { }

This is the error i am facing -这是我面临的错误 -

No qualifying bean of type 'package.TestDaoRepo' available: expected at least 1 bean which qualifies as autowire candidate.没有可用的“package.TestDaoRepo”类型的合格 bean:预计至少有 1 个 bean 有资格作为自动装配候选。 Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}

Please help me where i am doing wrong ?请帮助我哪里做错了?

@WebMvcTest won't instantiate @Repository beans. @WebMvcTest不会实例化@Repository豆。 This is intentional in order to make the tests more lightweight and isolate the controller tests.这样做是为了使测试更轻量级并隔离控制器测试。 Typically, you would replace your services with mocks for such tests.通常,您会用模拟来替换您的服务以进行此类测试。

See the docs for explanation.请参阅文档以获取解释。

In your case, your test would contain something like:在您的情况下,您的测试将包含以下内容:

@ExtendWith(SpringExtension.class)
@WebMvcTest(TestCotroller.class)
class ITTestController {

@MockBean
private TestServiceInf serviceMock;

@Test
void test() {
    System.out.println("Test me ");
   // URL to call. controller and rest of the logic 
}

}

If you want to do integration tests, you shouldn't use @WebMvcTest .如果要进行集成测试,则不应使用@WebMvcTest

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NoSuchBeanDefinitionException:没有符合类型的合格bean <package> &#39;可用:至少有1个符合自动装配候选条件的bean - NoSuchBeanDefinitionException: No qualifying bean of type '<package>' available: expected at least 1 bean which qualifies as autowire candidate NoSuchBeanDefinitionException:没有可用类型的限定bean:预期至少有1个bean有资格作为autowire候选者 - NoSuchBeanDefinitionException: No qualifying bean of type available: expected at least 1 bean which qualifies as autowire candidate 没有可用的“javax.sql.DataSource”类型的合格 bean:预计至少有 1 个有资格作为自动装配候选者的 bean - No qualifying bean of type 'javax.sql.DataSource' available: expected at least 1 bean which qualifies as autowire candidate 没有可用类型的合格 bean:预计至少有 1 个 bean 有资格作为自动装配候选者 - No qualifying bean of type available: expected at least 1 bean which qualifies as autowire candidate 没有可用类型的合格Bean:预计至少有1个合格为自动装配候选的Bean - No qualifying bean of type available: expected at least 1 bean which qualifies as autowire candidate 没有可用的&#39;xxx.dao.AreaDao&#39;类型的合格Bean:预计至少有1个符合自动装配候选条件的Bean - No qualifying bean of type 'xxx.dao.AreaDao' available: expected at least 1 bean which qualifies as autowire candidate 没有可用的“ru.spb.repository.UserRepository”类型的合格 bean:预计至少有 1 个 bean 有资格作为自动装配候选 - No qualifying bean of type 'ru.spb.repository.UserRepository' available: expected at least 1 bean which qualifies as autowire candidate 没有可用类型的合格 bean - 预计至少有 1 个 bean 有资格作为自动装配候选 - No qualifying bean of type available - expected at least 1 bean which qualifies as autowire candidate 没有可用的“java.lang.String”类型的合格 bean:预计至少有 1 个有资格作为自动装配候选者的 bean。 依赖注解: - No qualifying bean of type 'java.lang.String' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: 没有可用的“xxx.xxx.xxx.xxx.MyUserDetailsService”类型的合格 bean:预计至少有 1 个有资格作为自动装配候选者的 bean - No qualifying bean of type 'xxx.xxx.xxx.xxx.MyUserDetailsService' available: expected at least 1 bean which qualifies as autowire candidate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM