Java 无法为Spring引导控制器创建JUnit测试

Java 无法为Spring引导控制器创建JUnit测试,java,spring-boot,jpa,junit,Java,Spring Boot,Jpa,Junit,我正在尝试为SpringBoot应用程序中的一个简单控制器编写一个测试。但是,由于TopicRepository和TopicController的bean创建,我收到了错误。我参考了一个教程,对Spring boot开发还不太熟悉,所以不确定它到底是如何工作的。我怎样才能使测试有效 控制器测试 @RunWith(SpringRunner.class) @WebMvcTest(TopicController.class) public class TopicControllerTest {

我正在尝试为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())));

        }       
}
@RunWith(SpringRunner.class)
@WebMvcTest(TopicController.class)
公共类TopicControllerTest{
@自动连线
私有MockMvc;
@蚕豆
私人TopicService TopicService;
@试验
返回时的公共无效JSONARRAY()
抛出异常{
主题=新主题(“b”、“b名称”、“b描述”);
List allTopics=new ArrayList();
添加(主题);
Mockito.when(topicService.getAllTopics())。然后返回(allTopics);
mvc.perform(get(“/topics”)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(状态().isOk())
.andExpect(jsonPath(“$[0].id”)是(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);
    }

}

@RestController
公共类主题控制器{
@自动连线
私有TopicService TopicService;//将TopicService作为依赖项注入
@自动连线
私人专题报道专题报道;
@请求映射(“/topics”)
公共列表getAllTopics(){
返回topicService.getAllTopics();
}
@请求映射(“/topics/{name}”)
公共主题getTopic(@PathVariable字符串名称){
返回topicService.getTopic(名称);
}
@RequestMapping(method=RequestMethod.POST,value=“/topics”)
public void addTopic(@RequestBody Topic){
topicService.addTopic(topic);
}
@RequestMapping(method=RequestMethod.PUT,value=“/topics/{Id}”)
public void updateTopic(@RequestBody主题,@PathVariable字符串Id){
topicService.updateTopic(主题,Id);
}
}
专题报告

public interface TopicRepository extends CrudRepository<Topic, String>{

}

公共接口TopicRepository扩展了crudepository{
}
我得到的错误是


UnsatifiedDependencyException:创建名为“topicController”的bean时出错:通过字段“topicRepository”表示未满足的依赖关系;嵌套异常为org.springframework.beans.factory.NoSuchBean定义异常:没有类型为“io.nkamanoo.springbootstarter.repository.TopicRepository”的合格bean可用:至少需要1个符合autowire候选条件的bean。依赖项注释:{@org.springframework.beans.factory.annotation.Autowired(required=true)}

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

在代码中:

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

您没有在测试中定义该回购协议。只需使用@Mock注释在测试类中声明它。