In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "Java to achieve SpringBoot on Controller unit test code how to write", in the daily operation, I believe that many people in the Java implementation of SpringBoot on Controller unit test code on how to write questions, editor looked up all kinds of information, sorted out a simple and easy to use method of operation, hope to answer the "Java implementation of SpringBoot on Controller unit test code how to write" the doubt is helpful! Next, please follow the editor to study!
Controller Code package com.keafmd.controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;import java.util.Map / * Keafmd * * @ ClassName: HelloController * @ Description: * @ author: cozy Conan * @ Date: 2021-04-02 9:42 * @ Blog: https://keafmd.blog.csdn.net/ * / @ RestControllerpublic class HelloController {@ RequestMapping ("/ hello") Map hello () {Map map = new HashMap (); map.put ("keafmd", "cozy Conan"); map.put ("success", true); return map }} Unit Test Code package com.keafmd;import com.keafmd.SpringBoot02Application;import com.keafmd.controller.HelloController;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.http.MediaType;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.web.WebAppConfiguration Import org.springframework.test.web.servlet.MockMvc;import org.springframework.test.web.servlet.MvcResult;import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;import org.springframework.test.web.servlet.result.MockMvcResultHandlers;import org.springframework.test.web.servlet.result.MockMvcResultMatchers;import org.springframework.test.web.servlet.setup.MockMvcBuilders;import org.springframework.web.context.WebApplicationContext / * Keafmd * * @ ClassName: MvcTest * @ Description: * @ author: cozy Conan * @ Date: 2021-04-02 10:59 * @ Blog: https://keafmd.blog.csdn.net/ * / @ SpringBootTest (classes = SpringBoot02Application.class) @ AutoConfigureMockMvc / / equivalent to constructing a mvc object public class MvcTest {/ / simulated access Controller @ Autowired MockMvc mvc using context context Test public void test () throws Exception {MvcResult result = mvc.perform (MockMvcRequestBuilders.get ("/ hello"). Accept (MediaType.APPLICATION_JSON). AndExpect (MockMvcResultMatchers.status (). IsOk ()). AndDo (MockMvcResultHandlers.print ()) .andReturn ();}} garbled resolution
Replace the comment with: ↓
@ RequestMapping (value = "/ hello", produces = {"application/json;charset=UTF-8"})
HelloController:
Package com.keafmd.controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;import java.util.Map / * Keafmd * * @ ClassName: HelloController * @ Description: * @ author: cozy Conan * @ Date: 2021-04-02 9:42 * @ Blog: https://keafmd.blog.csdn.net/ * / @ RestControllerpublic class HelloController {@ RequestMapping (value = "/ hello", produces = {"application/json;charset=UTF-8"}) / / @ RequestMapping ("/ hello") Map hello () {Map map = new HashMap () Map.put ("keafmd", "Cool Conan"); map.put ("success", true); return map;}} this is the end of the study on "how to write unit test code for Controller by Java implementation SpringBoot", hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.