- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在 BookRestController
中测试我的休息端点。我使用 @WebMvcTest
编写了一个测试。
@RunWith(SpringRunner.class)
@WebMvcTest(BookRestController.class)
public class BookRestControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private CategoryService categoryService;
private ObjectMapper objectMapper = new ObjectMapper();
@Test
public void should_create_new_category_when_try_to_update() throws Exception {
given(categoryService.getCategoryById(20L)).willReturn(null);
Category category = new Category("Fantastyka");
ResultActions resultActions = mockMvc.perform(put("/api/category/10")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(category)));
resultActions.andExpect(status().isNoContent());
}
}
在存储库包中,我有 4 个存储库,但在我的测试端点中,我只使用其中之一。
这是我来自 BookRestController
@RestController
public class BookRestController {
@Autowired
private CategoryService categoryService;
@RequestMapping(value = "/api/category/{id}", method = RequestMethod.PUT)
public ResponseEntity<Category> updateCategory(@PathVariable Long id, @RequestBody Category category){
return ResponseEntity
.status(HttpStatus.NO_CONTENT)
.body(categoryService.updateCategory(id, category));
}
}
现在当我运行测试时出现错误
为什么?我在测试中没有使用Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'demoApplication': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.sda.spring.demo.repository.BookRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
BookRepository
。我该如何解决这个问题?
完整日志:
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125) at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108) at org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener.postProcessFields(MockitoTestExecutionListener.java:99) at org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener.injectFields(MockitoTestExecutionListener.java:79) at org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener.prepareTestInstance(MockitoTestExecutionListener.java:54) at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'demoApplication': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.sda.spring.demo.repository.BookRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {} at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:767) at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:218) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1308) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1154) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:846) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:127) at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99) at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:117) ... 25 more Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.sda.spring.demo.repository.BookRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1646) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1205) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1166) at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:855) at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:758) ... 43 more
Spring Boot:v2.1.0.RELEASE
编辑:有用。我想念我的 DemoApplication
中的 @Autowire
BookRepository
...我删除了这段代码,现在它可以工作了
@Autowired
private ApplicationContext applicationContext;
private BookRepository bookRepository;
private CategoryRepository categoryRepository;
@Autowired
public DemoApplication(BookRepository bookRepository, CategoryRepository categoryRepository) {
this.bookRepository = bookRepository;
this.categoryRepository = categoryRepository;
}
最佳答案
您的主应用程序类将加载任何定义的 bean,或通过注释启用的功能。
例如@EnableSchedling
、@ComponentScan
、@Import
等
将在切片测试中全部加载。
有关更多详细信息,请参阅此文档,
If you structure your code in a sensible way, your @SpringBootApplication class is used by default as the configuration of your tests.
It then becomes important not to litter the application’s main class with configuration settings that are specific to a particular area of its functionality.
关于java - @WebMvcTest 测试类中的 NoSuchBeanDefinitionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53277665/
我正在获取 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'spring
我创建了一个库,用于在多个 Spring Boot 应用程序上共享代码。 该库包含一个 Repository 类 RequestRepository。将库添加到 Spring Boot 项目后,编译并
所以我尝试使用在另一个类中生成的 bean 以在主应用程序中使用 package com.simon.spring.basics.properties; import org.spri
我使用 spring-data-jpa。没有 Spring Boot。 package ru.spb.repository; public interface CrudUserRepository e
我尝试使用ClassPathXmlApplicationContext获取bean。但我收到:NoSuchBeanDefinitionException:未定义名为“newsController”的
请解释以下有关 Spring 中 NoSuchBeanDefinitionException 异常的信息: 是什么意思? 什么情况下会抛出? 我该如何预防? 这篇文章旨在对使用 Spring 的应用程
我刚刚使用 Spring Boot V1.3.5 设置了一个新项目,在尝试将存储库 Autowiring 到服务中时,我不断收到 NoSuchBeanDefinitionException 异常。这很
我在 Spring mvc 中遇到错误: (org.springframework.beans.factory.BeanCreationException:创建名称为“webController”的b
我正在使用 spring mvc 开发 Web 应用程序。我想将 spring bean 注入(inject)到我的 Servlet Filter 中。 我引用了这个教程http://www.dead
我们有一个(非 Web 应用程序)Spring 应用程序,在我们的 CruiseControl 持续集成 Linux 盒子上运行测试时会抛出 NoSuchBeanDefinitionException
我想在 BookRestController 中测试我的休息端点。我使用 @WebMvcTest 编写了一个测试。 @RunWith(SpringRunner.class) @WebMvcTest(B
我正在尝试按照官方教程开发我的第一个 Spring MVC 应用程序。我从模板创建了 MVC 项目,然后添加了以下 Spring Security 配置。 pom.xml: ... ...
我正在尝试使用 @RestClientTest 来测试休息客户端类。 据说: It will apply only configuration relevant to rest client test
我想使用ElasticSearch和Spring Data。我添加了此依赖项: org.springframework.boot spring-boot-starter-data-e
当我运行"grails dbm-update --dataSource=production"时,出现以下异常: org.springframework.beans.factory.NoSuchBea
我有一个 Java 桌面应用程序,它通过 Hibernate 连接到 MySql 数据库。 我有以下配置文件: pom.xml,我在其中放置了一些内容和数据库配置文件 [app_name]_conte
我有一个使用 Quartz 调度程序的 Spring Boot (2.1.3) 项目。它是通过启动器包含的: dependencies { ... implementation('or
我正在从事 Spring Hibernate 项目。如果我没有将 bean 名称与 @Repository 或 @Component 一起提及,我会收到错误“NoSuchBeanDefinitionE
我的多模块项目结构如下: project-example (parent) -- project-example-domain: contains DAOs and Services -- proje
我有一个 Spring Boot 应用程序。当我调用 context.getBean(MyController.class) 时,它工作正常。当我调用 context.getBean("MyContr
我是一名优秀的程序员,十分优秀!