首页 > 解决方案 > Mockito - doCallRealMethod 在springboot应用程序中没有按预期工作

问题描述

对于具有字符串参数的方法,mockito 中的 doCallRealMethod 没有按预期工作(即它被模拟而不是调用真实方法)。我有以下课程

Class A{
  @Autowired
  Utils utils;


  public invokerMethod(String a,String b,String c){
      utils.realMethod(a,b,c);
      .....
      ......   

 }
}

类实用程序:

Class Utils throws Exception{
        public void realMethod(String a,String b,String c)     
             if(a.equals(""))
                 throw someException();
   }

测试类:

@RunWith(SpringRunner.class)
@SpringBootTest
Class ATest(){
    @Autowired
    A a;

   @MockBean
   Utils utils;

   @Test
public invokerMethodTest(){
doCallRealMethod().when(utils).realMethod(anyString(),anyString(),anyString());
      a.invokerMethod("","","");
 }
}

我尝试用“”替换anyString,new String()但它仍然不适合我

标签: javaspring-bootjunitmockingmockito

解决方案


推荐阅读