1.開始之前
- IDE:Intellij idea
- 依賴jar包 : spring-boot-starter-test
- 生成測試類
快捷方式:鼠標(biāo)移動到要測試的類上 按下 alt+enter
需要的注解:
@RunWith(SpringRunner.class)
@SpringBootTest
@Test
ex:
@RunWith(SpringRunner.class)
@SpringBootTest
public class ClassServiceTest {
@Test
public void addInfo()
{
}
}
3.使用斷言assertThat
assertThat 基本語法
assertThat( [value], [matcher statement] );
- value 是接下來想要測試的變量值拣宰;
- matcher statement 是使用 Hamcrest 匹配符來表達的對前面變量所期望的值的聲明,如果 value 值與 matcher statement 所表達的期望值相符办成,則測試成功门躯,否則測試失敗年局。
- Hamcrest 匹配符
API:http://hamcrest.org/JavaHamcrest/javadoc/2.0.0.0/
5.Controller單元測試
上面只是針對Service層做測試炮车,但是有時候需要對Controller層(API)做測試映屋,這時候就得用到MockMvc了崎岂,你可以不必啟動工程就能測試這些接口。
MockMvc實現(xiàn)了對Http請求的模擬,能夠直接使用網(wǎng)絡(luò)的形式絮蒿,轉(zhuǎn)換到Controller的調(diào)用尊搬,這樣可以使得測試速度快、不依賴網(wǎng)絡(luò)環(huán)境土涝,而且提供了一套驗證的工具毁嗦,這樣可以使得請求的驗證統(tǒng)一而且很方便。
Controller類:
package com.dudu.controller;
/** 教程頁面
* Created by tengj on 2017/3/13.
*/
@Controller
@RequestMapping("/learn")
public class LearnController extends AbstractController{
@Autowired
private LearnService learnService;
private Logger logger = LoggerFactory.getLogger(this.getClass());
@RequestMapping("")
public String learn(Model model){
model.addAttribute("ctx", getContextPath()+"/");
return "learn-resource";
}
/**
* 查詢教程列表
* @param page
* @return
*/
@RequestMapping(value = "/queryLeanList",method = RequestMethod.POST)
@ResponseBody
public AjaxObject queryLearnList(Page<LeanQueryLeanListReq> page){
List<LearnResource> learnList=learnService.queryLearnResouceList(page);
PageInfo<LearnResource> pageInfo =new PageInfo<LearnResource>(learnList);
return AjaxObject.ok().put("page", pageInfo);
}
/**
* 新添教程
* @param learn
*/
@RequestMapping(value = "/add",method = RequestMethod.POST)
@ResponseBody
public AjaxObject addLearn(@RequestBody LearnResource learn){
learnService.save(learn);
return AjaxObject.ok();
}
/**
* 修改教程
* @param learn
*/
@RequestMapping(value = "/update",method = RequestMethod.POST)
@ResponseBody
public AjaxObject updateLearn(@RequestBody LearnResource learn){
learnService.updateNotNull(learn);
return AjaxObject.ok();
}
/**
* 刪除教程
* @param ids
*/@Test
@Transactional
public void add(){
LearnResource bean = new LearnResource();
bean.setAuthor("測試回滾");
bean.setTitle("回滾用例");
bean.setUrl("http://tengj.top");
learnService.save(bean);
}
@RequestMapping(value="/delete",method = RequestMethod.POST)
@ResponseBody
public AjaxObject deleteLearn(@RequestBody Long[] ids){
learnService.deleteBatch(ids);
return AjaxObject.ok();
}
/**
* 獲取教程
* @param id
*/
@RequestMapping(value="/resource/{id}",method = RequestMethod.GET)
@ResponseBody
public LearnResource qryLearn(@PathVariable(value = "id") Long id){
LearnResource lean= learnService.selectByKey(id);
return lean;
}
}
6.單元測試回滾
單元個測試的時候如果不想造成垃圾數(shù)據(jù)回铛,可以開啟事物功能,記在方法或者類頭部添加@Transactional注解即可,如下:
@Test
@Transactional
public void add(){
LearnResource bean = new LearnResource();
bean.setAuthor("測試回滾");
bean.setTitle("回滾用例");
bean.setUrl("http://tengj.top");
learnService.save(bean);
}