一起学Mockito(一)
写点笔记
共 4366字,需浏览 9分钟
·
2022-01-13 17:51
import org.junit.Test;
import org.springframework.web.servlet.ModelAndView;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class HomeControllerTest {
@Test
public void index() {
HomeController homeController =mock(HomeController.class);
//打桩
when(homeController.index()).thenReturn(new ModelAndView("index"));
ModelAndView modelAndView = homeController.index();
System.out.println(modelAndView.getViewName());
assertSame(modelAndView.getViewName(),"index");
}
}
@Test
public void index() {
HomeController homeController =mock(HomeController.class);
when(homeController.index()).thenReturn(new ModelAndView("index")).thenReturn(new ModelAndView("zhang"));
ModelAndView modelAndView = homeController.index();
System.out.println(modelAndView.getViewName());
assertSame(modelAndView.getViewName(),"index");
modelAndView = homeController.index();
System.out.println(modelAndView.getViewName());
Mockito.verify(homeController,times(2)).index();
}
@Test
public void dapan() {
ModelAndView modelAndView=null;
//spy用来创建一个实体对象,会执行指定的调用方法。
HomeController homeController = spy(HomeController.class);
//在调用层设置mock对象,mock用来创建一个假对象,执行方法的时候什么都不做,直接返回值
StockService stockService = mock(StockService.class);
homeController.setStockService(stockService);
List
result = new ArrayList<>(); List
candidateStockPos=new ArrayList<>(); result.add(new ShowTimeVo("2022-01-10",1));
result.add(new ShowTimeVo("2022-01-09",1));
result.add(new ShowTimeVo("2022-01-08",1));
CandidateStockPo candidateStockPo = new CandidateStockPo();
candidateStockPo.setWenyuRiver(0);
candidateStockPo.setShowCode("sh.00001");
candidateStockPo.setIndustry("1");
candidateStockPo.setCv(1);
candidateStockPo.setZsm(1);
candidateStockPos.add(candidateStockPo);
//对stockservice进行打桩
when(stockService.queryShowTime()).thenReturn(result);
//进行参数匹配
when(stockService.stockToday(anyString(),anyList())).thenReturn(candidateStockPos);
modelAndView=homeController.today("1","");
System.out.println(modelAndView.getViewName());
assertSame("today",modelAndView.getViewName());
}
4、.验证函数的确切、最少、从未调用次数
//at least
Mockito.verify(stockService,atLeast(1)).queryShowTime();
//at most
Mockito.verify(stockService,atMost(1)).queryShowTime();
//equals
Mockito.verify(stockService,times(1)).queryShowTime();
public void today() {
HomeController homeController = spy(HomeController.class);
StockService stockService = mock(StockService.class);
homeController.setStockService(stockService);
when(stockService.tongji(anyString())).thenThrow(new Exception("123"));
try {
homeController.today("1","1");
}catch (Exception e){
e.printStackTrace();
}
}
@Test
public void about() {
//create empty object
List tianjingle = mock(List.class);
//
tianjingle.add("one");
tianjingle.add("two");
InOrder inOrder = inOrder(tianjingle);
inOrder.verify(tianjingle).add("one");
inOrder.verify(tianjingle).add("two");
List firstMock = mock(List.class);
List secondMock = mock(List.class);
firstMock.add("one");
secondMock.add("1");
InOrder inOrder1 = inOrder(firstMock, secondMock);
inOrder1.verify(firstMock).add("one");
inOrder1.verify(secondMock).add("1");
}
评论