簡體   English   中英

MockMvc-將對象從模擬傳遞到控制器

[英]MockMvc - Pass an object from the mock to the controller

我正在對正在開發的Web應用程序進行單元測試,並且在測試控制器的某些功能時遇到了問題。

基本上,我正在創建一個mockMvc,我想將先前創建的對象傳遞給它。 代碼是這樣的:

        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())
        ;

控制器每個參數接收兩個對象。 該控制器是使用視圖創建指示對象的新記錄的控制器。

    @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";
    }

我已經驗證了,如果在addConnection()方法中放置了@RequestBody標記,則該測試對我有效,但它從Web停止工作。 我該如何模擬控制器通過模擬Mvc接收到的對象?

從視圖來看,我正在使用“ thymeleaf”從HTML表單生成新記錄。 來源是這樣的:

        <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">&times;</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>

提前非常感謝您。

顯然,HTML表單將值作為參數發送給控制器。 @M注釋,當更改嘲笑Mvc以將值作為參數而不是作為json主體傳遞時。 Deinum已修復該錯誤。

代碼如下:

        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())
        ;

您應該使用@RequestBody。 為什么停止工作? 請說明問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM