微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法测试我的@RestController:嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:

如何解决无法测试我的@RestController:嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:

Spring Boot应用。

我的控制器:

@RestController
public class ProductsController {
    private ProductRepository productRepository;

   @GetMapping("/products")
    public List<Product> getAllProducts() {

这是我对Rest控制器的测试:

import org.junit.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit4.springrunner;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
@RunWith(springrunner.class)
public class ProductRestControllerIntegrationTest {
    @Autowired
    private ProductRepository productRepository;

    @InjectMocks
    ProductsController productsController;

    @Test
    public void testFindAll() {

但是启动测试testFindAll时出现运行时错误

21:41:37.363 [Test worker] DEBUG org.springframework.test.context.cache - Spring test ApplicationContext cache statistics: [DefaultContextCache@81715d3 size = 1,maxSize = 32,parentContextCount = 0,hitCount = 0,missCount = 1]
21:41:37.375 [Test worker] ERROR org.springframework.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@4319ed88] to prepare test instance [com.myproject.ProductRestControllerIntegrationTest@63c46458]
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.myproject.ProductRestControllerIntegrationTest': Unsatisfied dependency expressed through field 'productRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDeFinitionException: No qualifying bean of type 'com.myproject.repo.ProductRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessproperties(AutowiredAnnotationBeanPostProcessor.java:399)
    at org.springframework.beans.factory.support.AbstractAutowireCapablebeanfactory.populateBean(AbstractAutowireCapablebeanfactory.java:1420)

解决方法

尝试在@RunWith(SpringRunner.class)下将@SpringBootTest添加到您的测试配置中

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。