alibaba / testable-mock

换种思路写Mock,让单元测试更简单
https://alibaba.github.io/testable-mock/
MIT License
1.83k stars 310 forks source link

接口实现的情况,方法A调用方法B,方法B无法被mock #260

Closed zheng594 closed 2 years ago

zheng594 commented 2 years ago

有一个InterfaceA接口,有方法A和方法B,大概代码如下

@Service
public  ServiceA implement InterfaceA{
     public void getA() {
         return this.getB();
     }

     public String getB() {
         return "A";
     }
}

public class Test {
      @Autowired
       private ServiceA serviceA;

       public static class Mock {
              @MockInvoke(targetClass = InterfaceA.class, targetMethod = "getB")
              public String getB() {
                return "B";
             }
          }

       @Test
       public void getA(){
             serviceA.getA(); // 这里返回的还是A,而不是B,getB方法并没有被mock掉
       }
}
linfan commented 2 years ago

在测试方法里的代码不会被Mock,文档有说明