gpt4 book ai didi

java - Spring Boot 测试 : Test Response throws "Could not find acceptable representation" , App 正常运行

转载 作者:行者123 更新时间:2023-11-28 20:14:12 25 4
gpt4 key购买 nike

我正在使用 Spring Boot 创建一个应用程序。当我运行我的应用程序并测试服务时,我发现这是正确的。

Picture of service running

但是,当我运行如下所示的测试时:

public class CatalogControllerTet {
private MockMvc mvc;
@Autowired
private WebApplicationContext context;
@Autowired
private CatalogRepository catalogRepository;

@Before
public void setUp() throws Exception {
this.mvc = MockMvcBuilders.webAppContextSetup(this.context).build();
}

@Test
public void getHello() throws Exception {

MvcResult r = mvc.perform(MockMvcRequestBuilders
.get("/catalogs")
.accept(MediaType.APPLICATION_JSON)).andReturn();
String content = r.getResponse().getContentAsString();
int i = 100;
}
}

当我在测试中放置断点时,我看到了这个。我注意到的一件事是,我在“supportedMediaTypes”列表中没有看到任何提及 JSON 的内容。

我见过其他一些与我类似的问题,但它们被追溯到没有任何 getters/setters 或与 Jackson 库有关的响应对象。服务启动并正确运行的事实意味着不是那些问题。

enter image description here

非常感谢对此的任何帮助。

最佳答案

我将@DataJpaTest 作为注释列在集成测试文件中。为什么会显示为这个错误,我永远不会知道,但确实如此。

作品:

@RunWith(SpringRunner.class)
@SpringBootTest
public class CatalogControllerITTest {
...

不起作用:

@RunWith(SpringRunner.class)
@SpringBootTest
@DataJpaTest
public class CatalogControllerITTest {
...

非常、非常令人惊讶和阴险的行为,因为永远不会怀疑注释对 servlet 处理响应的方式有任何影响。

关于java - Spring Boot 测试 : Test Response throws "Could not find acceptable representation" , App 正常运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41419346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com