[英]MockMvc - Pass an object from the mock to the controller
I am doing unit tests of a web application that I am developing and I have encountered a problem when testing some of the functions of the controller. 我正在对正在开发的Web应用程序进行单元测试,并且在测试控制器的某些功能时遇到了问题。
Basically I am creating a mockMvc and I want to pass it an object that I previously created. 基本上,我正在创建一个mockMvc,我想将先前创建的对象传递给它。 The code is this:
代码是这样的:
Connection connection1 = new Connection();
connection1.setStatus(Status.IN);
connection1.setConnectionId("countingCamera1Conn");
connection1.setPath("urlPath");
connection1.setUsername("userName");
when(connectionRepoMock.existsById(anyString())).thenReturn(true);
//then
mockMvc.perform(post("/model/connection")
.content(asJsonString(connection1))
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isCreated())
;
The controller receives two objects per parameter. 控制器每个参数接收两个对象。 This controller is the one that uses the view to create a new record of the indicated object.
该控制器是使用视图创建指示对象的新记录的控制器。
@PostMapping("model/connection")
public String addConnection(Connection connection, Model model) {
checkRole(model);
if(!checkElement(connection,model))
return "error";
if(controllerRepo.existsById(connection.getConnectionId())) {
model.addAttribute("errorMsg", "The Id already exists, please try another one");
return "error";
}
controllerRepo.save(connection);
return "redirect:/model/connection";
}
I have verified that if in the addConnection()
method I put the @RequestBody
tag, the test works for me but it stops working from the Web. 我已经验证了,如果在
addConnection()
方法中放置了@RequestBody
标记,则该测试对我有效,但它从Web停止工作。 How can I simulate the object the controller receives with the mockMvc? 我该如何模拟控制器通过模拟Mvc接收到的对象?
From the view, I'm using "thymeleaf" to generate new records from HTML form. 从视图来看,我正在使用“ thymeleaf”从HTML表单生成新记录。 The source is like this:
来源是这样的:
<div class="form-group row px-md-4">
<h1>Connection</h1>
<div class="col-sm-1 py-sm-1">
<button class="btn btn-primary" type="button" data-toggle="modal"
data-target="#addConnection" value="Add a connection" rel="tooltip"
title="Add a new connection">
<i class="fa fa-plus"></i>
</button>
</div>
<!-- Modal -->
<div class="modal fade" id="addConnection" tabindex="-1"
role="dialog" aria-labelledby="#addConnectionTitle"
aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="addConnectionTitle">Add a
connection</h5>
<button type="button" class="close" data-dismiss="modal"
aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form th:action="@{/model/connection}" th:object="${connection}"
method="post">
<div class="form-group">
<label for="id_input" class="col-form-label"><b>ID:
*</b></label> <input type="text" class="form-control" id="id_input"
th:field="*{connectionId}" placeholder="Enter ID" required>
</div>
<div class="form-group">
<label for="path_input" class="col-form-label"><b>Path:
*</b></label> <input type="text" class="form-control" id="path_input"
th:field="*{path}" placeholder="Enter Path" required>
</div>
<div class="form-group">
<label for="username_input" class="col-form-label"><b>User
name: *</b></label> <input type="text" class="form-control"
id="username_input" th:field="*{username}"
placeholder="Enter User">
</div>
<div class="form-group">
<label for="pwd_input" class="col-form-label"><b>Password:
*</b></label> <input type="password" class="form-control" id="pwd_input"
th:field="*{pwd}" placeholder="Enter Password">
</div>
<div class="form-group">
<label for="status_input" class="col-form-label"><b>Status:
*</b></label> <select class="form-control" id="status_input"
th:field="*{status}">
<option
th:each="status : ${T(com.ascspain.iothub.model.Status).values()}"
th:value="${status}" th:text="${status}"></option>
</select>
</div>
<label class="text-muted px-sm-1"><small>Check
that all credentials with * are filled</small></label> <br>
<div class="modal-footer">
<button type="button" class="btn btn-secondary"
data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save
changes</button>
</div>
</form>
</div>
</div>
</div>
</div>
<!-- End of Modal -->
</div>
Thank you very much in advance. 提前非常感谢您。
Apparently, the HTML form sends the values as parameters to the controller. 显然,HTML表单将值作为参数发送给控制器。 When changing the mockMvc to pass the values as parameters instead of as json body, as commented by @M.
@M注释,当更改嘲笑Mvc以将值作为参数而不是作为json主体传递时。 Deinum has fixed the error.
Deinum已修复该错误。
The code would be as follows: 代码如下:
mockMvc.perform(post("/model/connection")
.param("connectionId", "countingCamera1Conn")
.param("path", "urlPath")
.param("status", "IN")
.param("username", "username")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isCreated())
;
You should use @RequestBody. 您应该使用@RequestBody。 Why it stops working?
为什么停止工作? Explain problem please
请说明问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.