SpringBoot 单元测试

1. 单元测试需添加标签,表示单元测试

@RunWith(SpringRunner.class)
@SpringBootTest

2.在测试的方法上加标签

@Test

3.测试 Service

package com.example;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.example.domain.service.IUserInfoService;

@RunWith(SpringRunner.class)
@SpringBootTest
public class TheSardineMerveilleApplicationTests {
	
	@Autowired
	private IUserInfoService iUserInfoService;
	
	@Test
	public void user() throws Exception {
	    iUserInfoService.getUserById(917);
	}
}

4.测试Controller  需要加类标签

@AutoConfigureWebMvc
package com.example;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureWebMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureWebMvc
public class TheSardineMerveilleApplicationTests {

	@Autowired
	/* 测试 controller */
	private MockMvc mvc;
	
	@Test
	public void contextLoads() throws Exception {
	    mvc.perform(MockMvcRequestBuilders.get("/url").contentType(MediaType.APPLICATION_JSON_UTF8))
	    // 判断返回状态 200 404
	    .andExpect(MockMvcResultMatchers.status().isOk())
	    // 返回值等于 什么
	    .andExpect(MockMvcResultMatchers.content().string("返回值"));
	}

}

5.测试Controller 携带参数

    (1). 单个参数传递

package com.example;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureWebMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureWebMvc
public class TheSardineMerveilleApplicationTests {
	
	@Autowired
	private WebApplicationContext context;
	 
	private MockMvc mvc;
	
	public void setUp(){
	    mvc = MockMvcBuilders.webAppContextSetup(context).build();
        }    
	
	@Test
	public void user() throws Exception {
	    setUp();
	    mvc.perform(MockMvcRequestBuilders.get("/url")
	    // 传递参数
	    .param(key, value))
	    // 判断返回状态 200 404
	    .andExpect(MockMvcResultMatchers.status().isOk())
	    // 返回值等于 什么
	    .andExpect(MockMvcResultMatchers.content().string("返回值"));
	}
}

/*
 * required 是否可为空
 * defaultValue  默认值
 */
@RequestMapping("/url")
public ResultVO<UserInfo> user(@RequestParam(name="key", required=false, defaultValue="value") String key){
    System.out.println(key);
}

(2).对象传值

package com.example;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureWebMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureWebMvc
public class TheSardineMerveilleApplicationTests {
	
	@Autowired
	private WebApplicationContext context;
	 
	private MockMvc mvc;
	
	public void setUp(){
	    mvc = MockMvcBuilders.webAppContextSetup(context).build();
        }
	
	@Test
	public void user() throws Exception {
	    setUp();
	    mvc.perform(MockMvcRequestBuilders.get("/url")
	    // 传递参数
	    .param(key, value))
	    .param(key, value))
	    .param(key, value))
	    .param(key, value))
	    .param(key, value))
	    .param(key, value))
	    // 判断返回状态 200 404
	    .andExpect(MockMvcResultMatchers.status().isOk())
	    // 返回值等于 什么
	   .andExpect(MockMvcResultMatchers.content().string("返回值"));
	}
}
@RequestMapping("/user")
public List<UserInfo> user(UserInfo userInfo) throws ExceptionHandleMes {
    System.out.println(userInfo);
    return null;
}

6.分页 及排序

Pageable 

需要有  size number  sort

package com.example;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureWebMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureWebMvc
public class TheSardineMerveilleApplicationTests {
	
	@Autowired
	private WebApplicationContext context;
	 
	private MockMvc mvc;
	
	public void setUp(){
	    mvc = MockMvcBuilders.webAppContextSetup(context).build();
        }
	
	@Test
	public void user() throws Exception {
	    setUp();
	    mvc.perform(MockMvcRequestBuilders.get("/url")
	    // 传递参数
	    .param(key, value))
	    .param(key, value))
	    .param(key, value))
	    .param("number", 5))
	    .param("sort", "age,desc"))
	    // 判断返回状态 200 404
	    .andExpect(MockMvcResultMatchers.status().isOk())
	    // 返回值等于 什么
	    .andExpect(MockMvcResultMatchers.content().string("返回值"));
	}
}
/***
 * @PageableDefault 默认分页信息
 * 
 */
@RequestMapping("/user")
public List<UserInfo> user(UserInfo userInfo, @PageableDefault(page=1, size=5, sort="user_id desc") Pageable pageable){
    System.out.println(userInfo);
    System.out.println(pageable);
    return null;
}


注:单元测试在程序打包的时候会自动进行。

执行下面命令跳过单元测试

mvn clean package -Dmaven.test.skip=true  

如果 MockMvc 注册失败,则使用第二种方法,使用时 先注册


@Autowired
/* 测试 controller */
private MockMvc mvc;
@Autowired
private WebApplicationContext context;
	 
private MockMvc mvc;
	
public void setUp(){
    mvc = MockMvcBuilders.webAppContextSetup(context).build();
 }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值