繁体   English   中英

无法为 Spring Boot 控制器创建 JUnit 测试

[英]Unable to create JUnit Test for Spring Boot Controller

我正在尝试为 SpringBoot 应用程序中的简单控制器编写测试。 但是,由于我的 TopicRepository 和 TopicController 的 bean 创建,我收到了错误。 我参考了一个教程,对 Spring Boot 开发并不陌生,所以不确定它是如何工作的。 我怎样才能使测试工作?

控制器测试

@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);
    }

}

主题库

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)}

您需要使用@SpringBootTest注释您的测试类,以便它创建所有定义的 bean 并启动您的应用程序以运行测试用例。

在您的代码中:

@RunWith(SpringRunner.class)
@WebMvcTest(TopicController.class)
@SpringBootTest
public class TopicControllerTest {
}

我正在尝试为SpringBoot应用程序中的简单Controller编写测试。 但是,由于为我的TopicRepository和TopicController创建了bean,我收到了错误消息。 我已经参考了一个教程,并且对Spring Boot开发并不陌生,所以不确定它的工作原理。 如何进行测试?

控制器测试

@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);
    }

}

主题库

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)}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM