0

Scenario is an interface of Cucumeber.api package.

I want to use this interface to get scenario name.

I am trying:

String string = () - > {
public String getName(Scenario scenario) {
                return scenario.getName().toString();
            }
});

but it is giving error, what am I missing here?

I am already following several posts (like below), but didn't what wrong I am doing:

  1. Lambda Expression is not working, getting terminated
paul
  • 4,333
  • 16
  • 71
  • 144
  • 1
    What error is it giving you? – Nicholas K May 01 '19 at 13:53
  • When I hover over `=` it says to add braces to complete body and when I add it says there extra braces added. I guess I am missing some syntax but can't figure where. `Syntax error, insert "}" to complete MethodBody` – paul May 01 '19 at 13:59
  • Few things `- >` should be `->`(without space) and why don't you right away assign the value to the variable as `scenario.getName().toString()`? – Naman May 01 '19 at 14:40
  • Nice catch @Naman, after removing space it says `The target type of this expression must be a functional interface`. Scenario is an interface, if I initialize it I have to implement all its methods, I don't want that. – paul May 01 '19 at 14:59
  • @paul you would get the name using the API from `Scenario` only using one of its implementations, isn't it? Apart from that, the RHS you've used represent a `FunctionalInterface` while the LHS a `String`, hence the mismatch. – Naman May 01 '19 at 15:09
  • @Naman that not right I guess. `1.` I'm using one of it's implementation only, see `scenario.getName()` `2.` RHS has return type String that's why no mismatch. What I guess problem is that this Interface has many implement-able methods, but for lambda to work it need to have only one e.g. `Runnable`. So does that mean `lambda` has a flaw? – paul May 01 '19 at 15:14
  • @paul simple question, is `Scenario` a [`FunctionalInterface`](https://stackoverflow.com/questions/36881826/what-is-use-of-functional-interface-in-java-8)? – Naman May 01 '19 at 15:17

0 Answers0