作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Spring Boot 创建一个应用程序。当我运行我的应用程序并测试服务时,我发现这是正确的。
但是,当我运行如下所示的测试时:
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 库有关的响应对象。服务启动并正确运行的事实意味着不是那些问题。
非常感谢对此的任何帮助。
最佳答案
我将@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/
我是一名优秀的程序员,十分优秀!