Spring boot test

UserAction.java

@RestController
@RequestMapping
public class UserAction {

    Logger log = LoggerFactory.getLogger(UserAction.class);

    @Autowired
    private UserService userService;

    @RequestMapping("/findUsers")
    @ResponseBody
    public Page<User> users(@PageableDefault Pageable pageable){
        System.out.println(pageable);
        Page<User> page = userService.findUsers(pageable);
        return page;
    }

    @RequestMapping("/saveUser")
    @ResponseBody
    public User saveUser(User user){
        return userService.save(user);
    }

    @RequestMapping("/indexPage")
    public ModelAndView indexPage(){
        ModelAndView view = new ModelAndView();
        view.addObject("pageType","common");
        view.setViewName("index");
        return view;
    }
}

Main8761.java


@Controller
@SpringBootApplication
public class Main8761 {

    public static void main(String[] args) {
        SpringApplication.run(Main8761.class, args);
    }
}

UserActionTest.java

import com.ums.Main8761;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
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.RequestBuilder;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Main8761.class)
@WebAppConfiguration
public class UserActionTest {

    @Autowired
    private WebApplicationContext webApplicationContext;

    private MockMvc mvc;

    @Before
    public void setup(){
        mvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
    }

    /**
         1、mvc.perform执行一个请求;
         2、MockMvcRequestBuilders.get("/findUsers")构造一个请求
         3、mvc.andExpect添加执行完成后的断言
         4、mvc.andDo添加一个结果处理器,表示要对结果做点什么事情,比如此处使用MockMvcResultHandlers.print()输出整个响应结果信息。
         5、mvc.andReturn表示执行完成后返回相应的结果。
     */

    @Test
    public void users() throws Exception {
        String response = mvc.perform(
                post("/findUsers")
                .contentType(MediaType.APPLICATION_JSON).param("size", "3"))
                .andDo(print())
                .andReturn()
                .getResponse()
                .getContentAsString();
        System.out.println("response:"+response);
    }

    @Test
    public void saveUser() throws Exception {
       MvcResult mvcResult = mvc.perform(post("/saveUser")
                .contentType(MediaType.APPLICATION_FORM_URLENCODED)
                .param("userName","test")
                .param("addr","china"))
               .andDo(print()).andReturn();
        System.out.println("result: "+mvcResult.getResponse().getContentAsString());
    }

    @Test
    public void indexPage() throws Exception {
        mvc.perform(get("/indexPage"))
                .andExpect(view().name("index"))  //期望返回的页面是index
                .andExpect(model().attributeExists("pageType"))  //期望视图中包含一个属性,pageType
                .andDo(print());
    }
}

User.groovy

import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.Id
import javax.persistence.Table

@Entity
@Table(name = "t_user")
class User {
    public @GeneratedValue @Id int id;
    public String userName;
    public String addr;
    User(){}
    User(int id, String userName, String addr) {
        this.id = id
        this.userName = userName
        this.addr = addr
    }
    void setUserName(String userName){
        this.userName = userName;
    }

    void setAddr(String addr){
        this.addr = addr;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值