java 8 lambda tech question for jsonpath library - java

I'm looking info about lambda although I'm not able to find something similar to below function. It belongs to class org.springframework.test.web.servlet.result.JsonPathResultMatchers , and ResultMatcher is a #FunctionalInterface, and result is type MvcResult and jsonPathHelper.doesNotExist return void
public ResultMatcher doesNotExist() {
return result -> jsonPathHelper.doesNotExist(getContent(result));
}
I call above one through
jsonPath("$._embedded" ).doesNotExist()
I have literally no idea about:
if jsonPathHelper.doesNotExist return void then why doesNotExist return ResultMatcher.
Class has anything similar to result, where is this argument come from?
Thanks

The lambda in your code:
result -> jsonPathHelper.doesNotExist(getContent(result));
is just a representation of a ResultMatcher since its a FunctionalInterface. You can look at it as if :
public ResultMatcher doesNotExist() {
return new ResultMatcher() {
#Override
public void match(MvcResult result) throws Exception {
jsonPathHelper.doesNotExist(getContent(result)); // returns void
}
};
}
if jsonPathHelper.doesNotExist return void then why doesNotExist
return ResultMatcher
Your method doesNotExist, just returns the functional interface in itself, which can thereafter be used to invoke its match function. Note that invocation would also be returning void.
Class has anything similar to result, where is this argument come
from?
If you look at the anonymous class above, with lambda representation the result becomes the argument of the match method within the ResultMatcher implementation.
So when you actually wish to access this implementation(or ResultMatcher in general), you would invoke the method as follows(simplified initializations):
ResultMatcher resultMatcher = doesNotExist(); // your method returns here
MvcResult result = new MvcResult(); // some MvcResult object
resultMatcher.match(result); // actual invocation

Related

Java Supplier<> defined as lambda. Need help understanding how this code even runs

Sorry for the somewhat unclear title but hopefully you'll see soon that it wasn't so easy to come up with a better one :)
So I have this interface that extends the Java Supplier #FunctionalInterface by defining one new method and also a default implementation of the Supplier.get() method. My default impl of .get() only wraps a call to the other method in some exception handling.
Then in my code I have different "versions" of this Supplier initialized using lambda notation.
Ex: SomeSupplier s = () -> doSomething();
Not sure why I even tried this because logically I don't understand how this even works, which it does. In my mind when I define my supplier using lambda like this I'm essentially overriding the Supplier.get() method. So how is it that in practice it seems to override my SomeSupplier.getSome() method? And leave the default impl of the .get() method intact?
What am I missing here?
Working example code:
public static void main(String[] args) throws InterruptedException {
SomeSupplier s = () -> getSomeOrException(); // "implements" the Supplier.get(), right?
for (int i = 0; i < 100; i++) {
System.out.println(s.get()); // => "Some!" or "null"
Thread.sleep(2);
}
}
private static String getSomeOrException() throws SomeCheckedException {
if (System.currentTimeMillis() % 10 == 0) {
throw new SomeCheckedException("10 %!");
}
return "Some!";
}
private interface SomeSupplier extends Supplier<String> {
#Override
default String get() {
try {
return getSome();
}
catch (SomeCheckedException e) {
return e.getMessage();
}
}
String getSome() throws SomeCheckedException; // How is this overridden/implemented?
}
private static class SomeCheckedException extends Exception {
public SomeCheckedException(String message) {
super(message);
}
}
}```
Your mistake is that assuming that if a Lambda of a Supplier implements get then a lambda of a SomeSupplier must also implement get.
But instead a Lambda will always implement the single abstract method of an interface* it's about to implement. In Supplier that's get. Your SomeSupplier however has implemented get (with a default method). Therefore getSome() becomes the single abstract method of the functional interface SomeSupplier. So this line:
SomeSupplier s = () -> getSomeOrException();
is roughly analogous to this:
SomeSupplier s = new SomeSupplier() {
String getSome() throws SomeCheckedException() {
return getSomeOrException();
}
};
Note that this implements getSome and not the underlying get method.
*: This is also why functional interfaces can only ever have one abstract method: there's no fallback logic to pick one option if more than one such method exists for a given target type.

How to verify lambda functions Mockito

I am trying to test following method:
public void execute(Publisher<T> publisher) throws Exception {
PublishStrategy<T> publishStrategy = publisher.getPublishStrategy();
publishStrategy.execute(publisher::executionHandler);
}
Following is the Junit code:
#Test
public void testExecute() throws Exception {
PublishStrategy<Event> publishStrategy = Mockito.mock(PublishStrategy.class);
Publisher<Event> publisher = Mockito.mock(Publisher.class);
Mockito.when(publisher.getPublishStrategy()).thenReturn(publishStrategy);
Mockito.doNothing().when(publishStrategy).execute(publisher::executionHandler);
PublishJob job = new PublishJob(publisher);
job.execute(publisher);
Mockito.verify(publishStrategy, Mockito.times(1)).execute(publisher::executionHandler);
}
On the verify method call, I am getting following mockito exception:
Argument(s) are different! Wanted:
publishStrategy.execute(
com.test.producer.jobs.PublishJobTest$$Lambda$3/1146825051#6f45df59
);
-> at com.test.producer.jobs.PublishJobTest.testExecute(PublishJobTest.java:23)
Actual invocation has different arguments:
publishStrategy.execute(
com.producer.jobs.PublishJob$$Lambda$2/1525409936#38e79ae3
);
-> at com.producer.jobs.PublishJob.execute(PublishJob.java:30)
I don't understand why mockito considers both the lambda's are different?
Update
I solved it without using Mockito.
Here's the other approach. Omitted empty overridden methods:
#Test
public void testExecute() throws Exception {
PublishStrategy<Event> publishStrategy = new PublishStrategy<Event>() {
#Override
public void execute(Consumer<List<Event>> handler) {
Assert.assertNotNull(handler);
}
};
Publisher<Event> publisher = new AbstractPublisher<Event>() {
#Override
public void init(PublishStrategy<Event> publishStrategy) {
this.publishStrategy = publishStrategy;
}
#Override
public void executionHandler(List<IngestEvent> items) {
}
#Override
public PublishStrategy<IngestEvent> getPublishStrategy() {
return this.publishStrategy;
}
};
publisher.init(publishStrategy);
PublishJob job = new PublishJob();
job.execute(publisher);
}
In Java, two objects aren't equal because they are instances of the same class! Objects are equal because calling a.equals(b) returns true!
In your case, that class PublishJob probably doesn't override the equals method. So, comparing two lambda instances results in false. And note: I really don't see how you could fix this by adding an equals method.
In other words: when you do publisher::executionHandler you create two different lambda instances. It doesn't matter that they will both make a call on the same object. You have two lambda instances. And they are simply not equal. And that Mockito method checks for equality.
I think one way how to test this: see if you can get that lambda to be executed. Then verify that the expected call takes place on that object.

Java Method in Method in Java

I was searching several documents for getting answers but I couldn't.
What I want to code is "Methods in Method". Below is example in java8 (Stream API)
private static int sumStream(List<Integer> list) {
return list.stream().filter(i -> i > 10).mapToInt(i -> i).sum();
}
In that codes, list call stream() method, stream() method called filter() method , mapToInt() method called sum() and return.
How can I code like this pattern?? Or, could you tell me search word in google or example codes please?
You are referring to chaining of method calls. This can be achieved when the methods of a class return the instance on which they were called.
For example:
public class A {
public A foo () {
// do something
return this;
}
public A bar () {
// do something
return this;
}
}
Then you can chain the method calls:
A a = new A();
a.foo().bar().foo();

How to chain BiFunctions?

I would like to chain BiFunctions, like in the method chainWanted in the code sample below.
BiFunction takes Function as a parameter of AndThen. is it possible to somehow chain BiFunctions ?
The code here doesn't compile because of this and I cannot cast BiFunction to Function.
import java.util.function.BiFunction;
import java.util.function.Function;
import org.openqa.selenium.remote.RemoteWebDriver;
public class Wf {
BiFunction<RemoteWebDriver, WfParams, RemoteWebDriver> init = this::init;
BiFunction<RemoteWebDriver, WfParams, RemoteWebDriver> wait = this::wait;
BiFunction<RemoteWebDriver, WfParams, RemoteWebDriver> chainNow = init
.andThen(d -> {
System.out.println("--------------");
return null;
});
BiFunction<RemoteWebDriver, WfParams, RemoteWebDriver> chainWanted = init
.andThen((BiFunction) wait);
public RemoteWebDriver init(RemoteWebDriver d, WfParams params) {
System.out.println("init(d, params)");
return d;
}
public RemoteWebDriver wait(RemoteWebDriver d, WfParams params) {
System.out.println("Wf.wait(d, params)");
return d;
}
public static void main(String[] args) throws Exception {
new Wf().start();
}
private void start() {
chainNow.apply(null, null);
}
}
Chaining of one Function to another works naturally because the return value of the first function is passed as the argument to the next function, and that function's return value is passed as the argument to the subsequent function, and so forth. This doesn't work naturally with BiFunction because they take two arguments. The first argument would be the return value from the previous function, but what would the second argument be? It also explains why BiFunction allows chaining with andThen to a Function instead of to another BiFunction.
This suggests, however, that it would be possible to chain one BiFunction to another if there were some way of providing the value for second argument. This can be done by creating a helper function that stores the value for the second argument in a local variable. Then, a BiFunction can be converted into a Function by capturing that local variable from the environment and using it as the second argument.
Here's what that would look like.
BiFunction<RemoteWebDriver, WfParams, RemoteWebDriver> chainWanted = this::chainHelper;
RemoteWebDriver chainHelper(RemoteWebDriver driver, WfParams params) {
return
init.andThen(rwd -> wait.apply(rwd, params))
.apply(driver, params);
}
// ...
chainWanted.apply(driver, params);
The chainHelper method holds the params argument for later capture. We call init.andThen() in order to do the chaining. But this requires a Function whereas wait is a BiFunction. Instead of using a method reference this::wait we use the lambda expression
rwd -> wait.apply(rwd, params)
which captures params from the lexical environment. This gives a lambda expression that takes a single argument and returns a single value, so it's now a Function that wraps the wait which is a BiFunction. This is an example of partial application or currying. Finally, we call the resulting BiFunction using apply(), passing the original arguments.
Where should the WfParams come from for the invocation of wait? If you mean to reuse the same WfParams for all the functions calls, just put WfParams as a class member variable instead of passing it to each function.
class Wf {
private final WfParams params;
public Wf(WfParams params) {
this.params = params;
}
UnaryOperator<RemoteWebDriver> init = this::init;
UnaryOperator<RemoteWebDriver> wait = this::wait;
Function<RemoteWebDriver,RemoteWebDriver> chain = init.andThen(wait);
RemoteWebDriver init(RemoteWebDriver d) {
// can use WfParams here
return d;
}
RemoteWebDriver wait(RemoteWebDriver d) {
// can use WfParams here
return d;
}
private void start() {
chain.apply(null);
}
public static void main(String[] args) {
new Wf(new WfParams()).start();
}
}
Is there a particular reason you want to use function chaining like that? Why not simply call init(...); wait(...); from start()?
I did something like this - created my custom BiFunction.
The idea being:
Return type is same as the second argument
First argument is passed internally to chained biFunction
public interface BiFunctionCustom<T, U> extends BiFunction<T,U,U> {
default BiFunctionCustom<T, U> andThen(BiFunctionCustom<T, U> after) {
Objects.requireNonNull(after);
return (T t, U u) -> after.apply(t, apply(t, u));
}
}

Mockito How to mock void method with output argument?

I have a void method "functionVoid" that informs a parameter.
public class MyMotherClass {
#Inject
MyClass2 myClass2
public String motherFunction(){
....
String test = "";
myClass2.functionVoid(test);
if (test.equals("")) {
IllegalArgumentException ile = new IllegalArgumentException(
"Argument is not valid");
logger.throwing(ile);
throw ile;
}
....
}
}
public class MyClass2 {
public void functionVoid(String output_value)
{ ....
output_value = "test";
....
}
}
How do I mock this method in the JUnit method my method "motherFunction"?
In my example, the "test" variable is still empty.
#RunWith(MockitoJUnitRunner.class)
public class MyMotherClassTest {
#Mock
private MyClass2 myClass2 ;
#InjectMock
private final MyMotherClass myMotherClass = new MyMotherClass ();
#Test
public void test(){
myMotherClass.motherFunction();
}
}
If you want to mock the return result of motherFunction then you need not worry about the internal implementation of the method (which ends up calling functionVoid). What you do need to do is provide Mockito with an instruction as to what to do when the method, motherFunction is invoked, this can be achieved via the when clause with syntax;
when(mockedObject.motherFunction()).thenReturn("Any old string");
If that misses the point of what you are attempting to achieve then look at how to mock void methods in the documentation and determine whether the use of doAnswer is applicable here, something like;
doAnswer(new Answer<Void>() {
#Override
public Void answer(InvocationOnMock invocation) throws Throwable {
String output_value = invocation.getArguments()[0];
output_value = "Not blank";
return null;
}
}).when(myClass2).functionVoid(anyString());
If you can change functionVoid() to accept a mutable object as the parameter, then you should be able to achieve what you want.
For example, if you change functionVoid() as follows:
public void functionVoid(StringBuilder output_value)
{ ....
output_value.append("test");
....
}
and invoke it in your motherFunction as follows:
public String motherFunction(){
....
StringBuilder test = new StringBuilder();
myClass2.functionVoid(test);
if (test.toString().equals("")) {
Now modifying OceanLife's answer above, you should be able to do the following:
doAnswer(new Answer<Void>() {
#Override
public Void answer(InvocationOnMock invocation) throws Throwable {
StringBuilder output_value = invocation.getArguments()[0];
output_value.append("Not blank");
return null;
}
}).when(myClass2).functionVoid(any(StringBuilder.class));
Of course, if you can change functionVoid(), you could also just make it return a String instead of void.
In my example, the "test" variable is still empty.
This is not a Mockito problem.
Take a look at this question and especially this answer.
The gist of it is that Java is pass by value (this is explained far better at the links above). Nothing in Mockito or Java will ever be able to make the test var anything other than an empty String. It's an empty String before the method call, and will be an empty String after the call.
You can change an object's state within a method (e.g. adding objects to a collection within a method) and see those changes when you exit the method, but you cannot change what object a var references within a method and expect those changes to "stick" once you exit the method. Strings however, are effectively immutable (no state to change), so you can't even do this.
Thus no modifications to test can be made within that method call.
If you want to check method someMethod(String arg) of object Obj then:
String argument = "";
Mockito.verify(Obj, Mockito.times(1)).someMethod(argument);
Obj has to be Mock or Spy.
This works when you want to check if proper argument was passed to void method.
If your method modifies somehow argument then you should use assertion:
someMethod(StringWrapper wrapper) that changes string.
// given
String argument = "a";
String expected = "a_changed";
String wrapped = new StringWrapper(a);
// when
someMethod(wrapped);
// then
Assert.assertEquals(wrapped.getString(), expected)
I am not sure if this what you were looking for?

Categories

Resources