Unlike other answers. I would like to answer your question on TDD.
IF your doubleMe as simple as you wrote, that is, clrealy you should stop abusing the reference to the method expression and just call it directly as a call to the general method.
IF your doubleMe so complex that you want to test doubleMe independent, you need to do an implicit explanation of the dependencies by injecting the dependencies to see if they can work together with their cummunication protocols. But java cannot reference the derctly method, except that you use api reflection of Method / using an anonymous class that implements SAM , which delegates the request to the method earlier in jdk-8. What a joyful thing - you can link to the method expression link to the functional interface in jdk-8. so you can make explicit dependencies explicit using the functional interface, then I would like to write some communication protocol tests, as shown below:
@Test void applyingMultiplicationWhenCalculating???(){ IntUnaryOperator multiplication = mock(IntUnaryOperator.class); B it = new B(multiplication); it.myCalculation(); verify(multiplication).applyAsInt(3); }
And then your classes, like B , for example, use dependency injection, as shown below:
public class B { IntUnaryOperator multiplication; public B(IntUnaryOperator multiplication){ this.multiplication = multiplication; } public void myCalculation() { multiplication.applyAsInt(3); } }
THEN, you can reuse a method by referencing a method expression reference to a functional interface, as shown below:
A a = new A(MyUtil::doubleMe); B b = new B(MyUtil::doubleMe);
source share