怎么在Springboot中使用Junit和5实现单元测试

  介绍

今天就跟大家聊聊有关怎么在Springboot中使用Junit和5实现单元测试,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

<强>隔离外部依赖

Case1。被测类中被@ autowired或@注解标注的依赖对象,如何控制其返回值

以被测方法MatchingServiceImpl。java的匹配(MatchingOrder buyOrder, MatchingOrder sellOrder)为例

被测类MatchingServiceImpl

public  class  MatchingServiceImpl  implements  MatchingService  {   private 才能static  final  Logger  log =, LoggerFactory.getLogger (MatchingServiceImpl.class);   @ autowired才能   private 才能;QuoteService  quoteService;   ,,…   public 才能;MatchingResult 匹配(MatchingOrder  buyOrder, MatchingOrder  sellOrder), {   ,,,int  currentPrice =, quoteService.getCurrentPriceByProduct (buyOrder.getProductCode ());   ,,,MatchingResult  result =, new  MatchingResult ();   ,,,if  (sellOrder  !=, null ,,, buyOrder  !=, null ,,   ,,,,,,,sellOrder.getPrice (), & lt;=, buyOrder.getPrice ()), {   ,,,…   ,,}   }

匹配方法中的quoteService.getCurrentPriceByProduct (buyOrder.getProductCode());要访问复述,获取当前报的价,这里我们需要把外部依赖quoteService模拟掉控制getCurrentPriceByProduct方法的返回值。使用5可以做到,具体如下:

测试类MatchingServiceImplTest

public  class  MatchingServiceImplTest  extends  MockitoBasedTest  {   ,/* *   ,,*,被@Mock标注的对象会自动注入到被@InjectMocks标注的对象中   ,,*/@Mock才能   private 才能;QuoteService  quoteService;   ,/* *   ,,*,& lt; pre>   ,,*,被测对象,用@InjectMocks标注,那些被@mock标注的对象就会自动注入其中。   ,,*,另一个注意点是这里的MatchingServiceImpl是直接新出来(Mockito  1.9版本后不新也可以),而不是通过春容器注入的,因为这里我不需要从春容器中   ,,*,获得其他依赖,不需要database  redis ,动物园管理员,mq,啥都不依赖,所以直接新的   ,,*,& lt;/pre>   ,,*/@InjectMocks才能   private 才能MatchingServiceImpl  matchingService =, new  MatchingServiceImpl ();   @Test才能   public 才能;void  testMatching_SuccessWhenCurrentPriceBetweenBuyPriceAndSellPrice (), {   ,,,MatchingOrder  buyOrder =, new  MatchingOrder ();   ,,,buyOrder.setPrice (1000);   ,,,buyOrder.setCount (23);   ,,,MatchingOrder  sellOrder =, new  MatchingOrder ();   ,,,sellOrder.setPrice (800);   ,,,sellOrder.setCount (20);   ,,,//,方法打桩(Method 存根)   ,,,//,当(x) .thenReturn (y),:当指定方法被调用时返回指定值   ,,,Mockito.when (quoteService.getCurrentPriceByProduct (Mockito.anyString ())) .thenReturn (900);   ,,,MatchingResult  result =, matchingService.matching (sellOrder buyOrder也);   ,,,org.junit.Assert.assertEquals(真的,,result.isSuccess());//,断言撮合是否成功   ,,,org.junit.Assert.assertEquals(20日,result.getTradeCount());//,断言成交数量   ,,,org.junit.Assert.assertEquals(900年,result.getTradePrice());,//,断言最新报价是否符合预期   ,,}

例2。被测函数一个调用被测类其他函数B,怎么控制函数B的返回值吗?

比如,MatchingServiceImpl中有个函数startBuyProcess,它里面调用了该类中的其他函数,如getTopSellOrder,匹配,如何控制这两个函数的返回值?
这里要解决的问题其实是怎么对一个类“部分模拟”——被测类的被测方法(如startBuyProcess)要真实执行,而另一些方法(如getTopSellOrder)则是要打桩(不真正进去执行)。

被测类MatchingServiceImpl

protected  void  startBuyProcess (MatchingOrder  buyOrder, boolean  waitForMatching), {   ,,,while (真实),{   ,,,,,//对手方最优价   ,,,,,MatchingOrder  topSellOrder =, getTopSellOrder (buyOrder.getProductCode ());   ,,,,,MatchingResult  MatchingResult =,匹配(buyOrder topSellOrder);   ,,,,,如果(matchingResult.isSuccess ()), {   ,,,,,,,doMatchingSuccess (buyOrder, topSellOrder, matchingResult MatchingType.BUY);   ,,,,,,,如果(buyOrder.getCount (), & lt;=, 0), {   ,,,,,,,,,休息;   ,,,,,,,}   ,,,,,}else  {   ,,,,,,,如果(waitForMatching), {   ,,,,,,,,,//加入待撮合队列   ,,,,,,,,,addToMatchingBuy (buyOrder);   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

怎么在Springboot中使用Junit和5实现单元测试