首页 > 解决方案 > 功能接口单元测试

问题描述

您如何测试使用功能接口的类。那么我如何测试下面的两个具体类:

@FunctionalInterface
public interface Test {
    int sumTwoValues(int x, int y);
}


public class UseTestExample {

   public int useTest(int z, Test test) {
     int returnValue = test.sumTwoValues(z, 10);

     return returnValue;
   }

}


public class MainTest {

  public void clientCall() {
    UseTestExample testExample = new UseTestExample();
   
    testExample.useTest(10, (x,y) -> {
        return x+y;
    });
  }
}

我可以看到 UseTestExample,我可以在单元测试期间传入一个实现并检查我返回的内容是否正确?

但是 MainTest 类,我怎么能测试呢?

标签: javafunctional-interface

解决方案


推荐阅读