无法为Spring Boot Controller创建JUnit测试 [英] Unable to create JUnit Test for Spring Boot Controller
问题描述
我正在尝试为SpringBoot应用程序中的简单Controller编写测试.但是,由于为我的TopicRepository和TopicController创建了bean,我收到了错误消息.我已经参考了一个教程,并且对Spring Boot开发并不陌生,所以不确定它是如何工作的.如何使测试正常进行?
I am trying to write a test for a simple Controller in SpringBoot application. However, I am receiving errors due to bean creations for my TopicRepository and TopicController. I had referenced a tutorial and am little new to Spring boot development so not sure exactly how it works. How can I make the test work?
ControllerTest
ControllerTest
@RunWith(SpringRunner.class)
@WebMvcTest(TopicController.class)
public class TopicControllerTest {
@Autowired
private MockMvc mvc;
@MockBean
private TopicService topicService;
@Test
public void whenGetTopics_returnJSONArray()
throws Exception {
Topic topic = new Topic("b","b name", "b descp");
List<Topic> allTopics = new ArrayList<>();
allTopics.add(topic);
Mockito.when(topicService.getAllTopics()).thenReturn(allTopics);
mvc.perform(get("/topics")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].id", is(topic.getId())));
}
}
控制器
@RestController
public class TopicController {
@Autowired
private TopicService topicService; //inject the topicService as a dependency
@Autowired
private TopicRepository topicRepository;
@RequestMapping("/topics")
public List<Topic> getAllTopics() {
return topicService.getAllTopics();
}
@RequestMapping("/topics/{name}")
public Topic getTopic(@PathVariable String name) {
return topicService.getTopic(name);
}
@RequestMapping(method=RequestMethod.POST, value= "/topics")
public void addTopic(@RequestBody Topic topic) {
topicService.addTopic(topic);
}
@RequestMapping(method=RequestMethod.PUT, value = "/topics/{Id}")
public void updateTopic(@RequestBody Topic topic, @PathVariable String Id){
topicService.updateTopic(topic, Id);
}
}
TopicRepository
TopicRepository
public interface TopicRepository extends CrudRepository<Topic, String>{
}
我遇到的错误是
UnsatisfiedDependencyException:创建名称为'topicController'的bean时出错:通过字段'topicRepository'表示的不满足的依赖关系;嵌套的异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型为'io.nkamanoo.springbootstarter.repository.TopicRepository'的合格bean:期望至少有1个可以作为自动装配候选的bean.依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}
UnsatisfiedDependencyException: Error creating bean with name 'topicController': Unsatisfied dependency expressed through field 'topicRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'io.nkamanoo.springbootstarter.repository.TopicRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
推荐答案
Springboot自动扫描所有遵循基本软件包名称的软件包. 例如,如果您的新springboot项目的软件包中包含 main 类, com.example.project .
Springboot automatically scans all packages that follows the base package name. Example, if your fresh springboot project has the main class residing in a package com.example.project.
然后,所有
entities,repository,service,controller
类都需要分组在遵循相同软件包结构的软件包中,如果您将类分组在软件包中,那么 springboot 会自动扫描当使用 @Autowired 进行依赖注入时,程序包将执行自动依赖注入.
class all need be group in a package which follows the same package structure, if you group your classes in packages, so springboot automatically scans the packages perform automatic dependency injection when, @Autowired is used to perform dependency injection.
因此,您应该将其他软件包名称分组如下:
So therefore, you other package names should be grouped as follows:
- com.example.project.entities
- com.example.project.repositories
- com.example.project.services
- com.example.project.controller
这解决了 {@ org.springframework.beans.factory.annotation.Autowired(required = true)}
这篇关于无法为Spring Boot Controller创建JUnit测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!