- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下 REST Controller
@Controller
@RequestMapping("/rest/transceptors")
public class TransceptorRestController
{
@Autowired
private TransceptorDao transceptorDao;
@RequestMapping(value="/get/{idTransceptor}", method=RequestMethod.GET)
public @ResponseBody Transceptor getOne(@PathVariable("idTransceptor") Long idTransceptor)
{
return transceptorDao.searchByIdTransceptor(idTransceptor);
}
}
此 Controller 在 JBoss 中运行时工作正常,结果符合预期。我使用 Postman(Google Chrome 的 REST 测试扩展),我可以在 XML 和 JSON 中获得正确的结果。
但是,我在使用 MockMVC 进行测试时遇到了问题。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
locations={
"classpath:test-servlet-context.xml"
})
@WebAppConfiguration
public class TransceptorRestControllerTest {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setup()
{
mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@Test
public void testRoot() throws Exception
{
mockMvc.perform(get("/")).
andExpect(status().isOk());
}
@Test
public void testGet() throws Exception
{
mockMvc.perform(get("/rest/transceptors/get/1"))
.andExpect(status().isOk())
.andDo(print())
.andExpect(model().attribute("name", equals("Test_Name_1")));
}
TestRoot 测试正常。但是,当我尝试使用 andExpect(model()... 我收到消息“No ModelAndView Found”
为 XML 或 JSON 的特定期望替换 model() 部分时,XML 和 JSON 字符串始终返回空。
我花了几天时间试图理解这一点,我对 Java 比较陌生,对 Spring 更陌生。你能告诉我在哪里可以解决这个问题吗?
作为附加信息,我在任何地方都放置了日志消息(使用 sfj4l),但是当使用 Junit 运行时,DAO 中的日志消息有效,测试模块本身中的日志消息有效,但我的 REST Controller 中的日志消息没有出现。
就像是匹配了GET函数,但是函数的内容一直没有执行,得到的响应是空的。尽管如此,我对 isOk() 的调用还是成功的。
最佳答案
“No ModelAndView Found”是正确的。使用@ResponseBody,返回值直接写入响应主体。没有模型,没有 View 分辨率等。
更一般地说,理想情况下,侧重于从客户的角度测试请求的结果。这包括响应 header 、正文和响应状态。测试其他通常对客户端不可见的结果,例如更谨慎地测试模型属性。
关于Spring 3.2.2 : MockMVC, 返回空结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15618037/
我有一个想要测试的 REST 端点。通过 POST 请求命中此端点会上传远程 git 存储库中的文件。我正在尝试使用mockMvc测试对此端点的POST调用(我只想查看返回状态“isOk()”)——我
当我使用 WebApplicationContext 创建 MockMvc 以使用 Spring MVC 3.2.3 进行 restful-webservice 测试时,如下所示: @Autowire
我尝试测试注入(inject) UserPrincipal 的 mvcControllers 的访问: restPockMockMvc .perform(get("/pocs").principal(
我有一个带有 MySQL 数据库的简单 REST 应用程序,一切正常,但是在测试时我们是否需要创建一个虚拟对象并对其进行测试,或者通过模拟数据库进行测试?虚拟对象有相当大的构造函数和嵌套类,这是一个很
我有一个使用 MockMvc 的 springBoot 2.1.9.RELEASE 应用程序。 我想知道是否有办法从文件中获取正文内容 mockMvc.perform(post("/hostel")
我想测试包含请求正文的请求类型 post 的其余 Controller 方法。所有其他获取方法在发布后都可以正常工作。我期待 200 OK 但它返回 417。我的目的是让它通过我运行测试但失败了。我是
我正在尝试添加 Cucumber到我已经在使用 spring-test 和 JUnit 的 Spring Web MVC 项目。我已经编写的非 Cucumber 集成测试 Autowiring 了 W
我想向 Controller 发送一个由文件和简单类型组成的复杂对象。 public class ContributionNew { private List elementsToAdd;
我正在为我的 Spring 数据休息存储库编写测试用例: @RepositoryRestResource(collectionResourceRel = "teams", path = "teams"
当我模拟传递有效id时,测试运行成功,但是当我传递无效id时,返回返回404,这是预期的,但没有响应,当我正常的Http请求时,它返回响应正文,消息未找到,状态404。我在这里做错了什么? @RunW
当我使用以下 Controller /测试配置收到“请求的映射错误”时,目前正在努力解决问题。 Controller : @Slf4j @Validated @RestController @Requ
在我的测试中,我设置了 MockMvc @Before 中的对象像这样 mockMvc = MockMvcBuilders.webAppContextSetup(context)
有人有任何提示,或者有人知道如何测试 HTTP 响应对象返回的“错误消息”吗? @Autowired private WebApplicationContext ctx; private MockMv
我正在为 Spring Rest 服务编写测试,它将 url 重定向到另一个 Spring 服务。所以目标是使用书签名称找到书签。第一个服务使用 Bookmark 名称获取 BookmarkId,然后
我需要测试一个调用异步服务的 Controller 。 Controller 代码 @RequestMapping(value = "/path", method = RequestMethod.PO
我刚刚使用 MockMvc 为 Controller 创建了一个简单的集成测试。一切正常,但即使 Controller 方法返回某些内容也没有任何响应。这是 Controller : import d
我编写了 Controller ,它为每个映射使用不同的值。现在我将其折射为所有映射使用相同的值,但是我不知道如何进行测试,因为它对每个映射都返回 404。 这是我的 Controller : @Cr
我想在 Spring Boot 中为我的 Controller 创建一些测试。具体来说,我想创建一个测试来处理表单以添加新项目。该项目属于 Drug 类,并且有一个集合作为 参数: @ManyToMa
使用 Spring boot 2 和 Spring mvc。我正在尝试使用 mockMvc 测试我的休息 Controller @PostMapping( value = "/
大多数时候,我们不是在普通的 JUnit 断言中添加注释,而是向断言添加一条消息,以解释为什么这是断言: Person p1 = new Person("Bob"); Person p2 = new
我是一名优秀的程序员,十分优秀!