【问题标题】:HttpMessageNotWritableException when testing with @SpringBootTest使用 @SpringBootTest 测试时出现 HttpMessageNotWritableException
【发布时间】:2022-01-17 18:10:18
【问题描述】:

我正在尝试测试基于 Spring Boot 的休息端点。代码能够返回预期的输出,但测试失败并出现以下错误:

已解决 [org.springframework.http.converter.HttpMessageNotWritableException: No converter for [class java.util.LinkedList] with preset Content-Type 'null']

对此的任何想法将不胜感激!

这是相同的代码:

控制器->

@RestController
public class SampleController {
    
    @Autowired
    StudentService studentService;
    
    @GetMapping(value="students",produces = "application/json")
    public ResponseEntity<List<Student>> getStudentDetails(){
        
    return new ResponseEntity<List<Student>>(studentService.getAllStudents(),HttpStatus.OK);
    }

}

服务类->

@Service
public class StudentService {
    
    private List<Student> listOfStudents;
    
    @PostConstruct
    public void init() {
        List<Student> list1= new LinkedList<Student>(); 
        Student s1 = new Student("a","S1");
        list1.add(s1);
        listOfStudents = list1;
    }
    
    public List<Student> getAllStudents(){
        return listOfStudents;
    }

}

波乔->

public class Student {

    private String sectionName;
    private String name;
    public String getRollNum() {
        return sectionName;
    }
    public void setRollNum(String rollNum) {
        this.sectionName = rollNum;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Student(String rollNum, String name) {
        this.sectionName = rollNum;
        this.name = name;
    }
    
    @Override
    public String toString() {
        return "Student [sectionName=" + sectionName + ", name=" + name + "]";
    }
    
    
}

测试->

@SpringBootTest(classes = {SampleController.class,StudentService.class})
@AutoConfigureMockMvc
public class SampleControllerTest {

    @Autowired
    private MockMvc mockMvc;
    
    @Autowired
    SampleController sampleController;
    
    @MockBean
    StudentService studentService;
    
    @Test
    public void getAllUsersTest() throws Exception{
        List<Student> mockedList = new LinkedList<Student>();
        Student dummyStudent = new Student("dummy","Dummy Student");
        mockedList.add(dummyStudent);
        Mockito.when(studentService.getAllStudents()).thenReturn(mockedList);
        mockMvc.perform(get("/students")).andExpect(status().isOk());
    }
}

【问题讨论】:

    标签: spring-boot-test


    【解决方案1】:

    当在@SpringBootTest 中手动配置classes 属性时,Spring Boot 的默认自动配置将退出,您将不会获得包含所有 bean 的 Spring TestContext:@SpringBootTest(classes = {SampleController.class,StudentService.class})。这样,您的上下文就缺少许多自动配置的 bean,例如 JSON 转换器。

    要么使用 @SpringBootTest 而不指定 classes 来编写与整个 Spring 上下文的集成测试,要么使用 @WebMvcTest

    我想对于您的测试场景,切片测试注释@WebMvcTest(SampleController.class) 是您正在寻找的。这将为您创建一个仅包含相关 Spring MVC 组件的切片 Spring TestContext。它也会auto-configure a MockMvc instance

    【讨论】:

    • 谢谢!这在一个示例中效果很好,但对于类似示例,测试失败并显示消息“无法找到@SpringBootConfiguration,您需要在测试中使用@ContextConfiguration 或@SpringBootTest(classes=...)。跨度>
    • 您能否用失败的“类似示例”更新您的问题。您是否也尝试使用@WebMvcTest 而不是@SpringBootTest
    猜你喜欢
    • 2020-05-02
    • 1970-01-01
    • 2020-01-10
    • 2019-09-23
    • 2017-05-28
    • 2017-07-24
    • 2023-03-14
    • 2019-11-07
    • 1970-01-01
    相关资源
    最近更新 更多