Spring boot 无法写入HTTP消息:org.springframework.HTTP.converter.HttpMessageNotWritableException

Spring boot 无法写入HTTP消息:org.springframework.HTTP.converter.HttpMessageNotWritableException,spring-boot,mockmvc,spring-boot-test,Spring Boot,Mockmvc,Spring Boot Test,在我的RestController上进行的一个简单测试失败得很惨 安装程序-SpringBoot 2.0.0.BUILD-SNAPSHOT,具有以下依赖项: dependencies { compile('org.springframework.boot:spring-boot-starter-data-jpa') compile('org.springframework.boot:spring-boot-starter-web') runtime('org.spring

在我的RestController上进行的一个简单测试失败得很惨

安装程序-SpringBoot 2.0.0.BUILD-SNAPSHOT,具有以下依赖项:

dependencies {
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-web')
    runtime('org.springframework.boot:spring-boot-devtools')
    runtime('com.h2database:h2')
    compileOnly('org.projectlombok:lombok')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}
城市目标:

@Getter
@Setter
@NoArgsConstructor
public class City {
    private Long id;
    private String city;

    public City(Long id, String city) {
        this.id = id;
        this.city = city;
    }
}
控制器类:

@RestController
@RequestMapping("/cities")
public class CityController {
    @GetMapping
    public List<City> findAllCities() {
        return Collections.singletonList(new City(1L, "Rancho Cordova"));
    }
}
运行测试时得到的异常:

.w.s.m.s.DefaultHandlerExceptionResolver : Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class java.util.Collections$SingletonList

我错过了什么?

我的错。我在上面犯的错误是,我为class参数指定了控制器。相反,代码应该是这样的:

@SpringBootTest(classes = CityApplication.class, webEnvironment = SpringBootTest.WebEnvironment.MOCK)

你甚至没有在问题中提到你的CityApplication类,这使得它不可能进行这样的调试。投票结束,因为“无法复制”。
@SpringBootTest(classes = CityApplication.class, webEnvironment = SpringBootTest.WebEnvironment.MOCK)