public class MyFunctions {
public Function<String, String> upperCaseFunction = str -> { return str.toUpperCase(); };
}
And lets say I use this function somewhere like so.
public class SomeComponent {
private MyFunctions myFunctions;
public String reverse(String input) {
Stream<String, String> stream = Stream.of(functions.upperCaseFunction);
return stream
.flatMap(function -> function.apply(input).... etc etc
}
}I now wish to test SomeComponent and mock MyFunctions.My problem is I can't figure out how to mock the Lambda expression in MyFunctions?I have tried the following so far and cannot seem to setup the lambda properly.@RunWith(MockitoJUnitRunner.class)
public class SomeComponentTest {private static final String TEST_STRING = "hello";
@Mock
private MyFunctions myFunctions;
@InjectMocks
private SomeComponent someComponent;
@Before
public void setUp() {
given(myFunctions.upperCaseFunction).thenReturn(Function<String, String>); // call to reverse throws MissingMethodInvocationException
given(myFunctions.upperCaseFunction.apply(TEST_STRING)).thenReturn("HELLO"); // call to upperCaseFunction.apply throws NullPointer
}
@Test
public void testReverse() {
String result = someComponent.reverse(TEST_STRING);
assert(result.equals("HELLO");
}
}Can anyone give me any advise on how to Mock MyFunctions and the lambda expression it contains?
--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at https://groups.google.com/group/mockito.
To view this discussion on the web visit https://groups.google.com/d/msgid/mockito/3f0ab191-0d04-4cd7-8bc6-24a541d951e1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
@Before
public void setUp() { myFunctions.upperCaseFunction = string -> { return "HELLO"; };
}
And then setting locally in each test where I wanted a different value.
@Test
public void testFailure {myFunctions.upperCaseFunction = string -> { return "xxx";};
// some assertions
}
To view this discussion on the web visit https://groups.google.com/d/msgid/mockito/62a48ff4-4ac4-4534-94b9-1c2486732640%40googlegroups.com.