How to get hold of a grid's row data?

For a unit-test I want to be able to choose a specific row - like grid.selectRow(5);.
However, I don’t have a handle on the objects that were assigned using setItems(…) during initialization, so I can’t refer to a specific item and then use grid.select(item);

I first thought that I could get at a list of the items (from which I could then pick one) via
List<Item> items = grid.getDataProvider().fetch(??).collect(Collectors.toList());
… but what has to go into the fetch query (marked with ?? here)?

Or is there some smarter solution? I was googling now for over an hour but haven’t found any workable solution. Is it really not possible to get a handle on a grid’s items via an index?

For a unit-test I want to be able to choose a specific row - like grid.selectRow(5);. However, I don’t have a handle on the objects that were assigned using setItems(…) during initialization, so I can’t refer to a specific item and then use grid.select(item);

Are you actually talking about integration test instead of a unit-test. GridElement in TestBench has method grid.selectRow(5); to select a row. So if your question is about TestBench it is about integration test / browser based testing. TestBench operates the Browser via Selenium, and thus do not have access directly to the session and data provider, because the session is running on different environment than the Browser.

In unit test you have access to DataProvider. Moreover in Vaadin 17 we introduced easier to use DataView API (there grid.setItems(…) returns instance of DataView), so that you do not need to use fetch() and provide Query object. DataView provides list of items as seen in the Grid with current filtering and sorting.

If you are using Vaadin 14, You can see Query object in use with fetch() for example here https://github.com/TatuLund/TwinColSelect/blob/master/src/main/java/org/vaadin/tatu/TwinColSelect.java#L333

On the other hand in unit test you cant do grid.select(5) as such method does not exists. In unit test you can test the internal server state of the component / view, but not its presentation on browser.

I hope this helps you to understand difference between unit tests and integration tests. Nejther of them can give full coverage.

Thanks for the response! I will try that out, soon.
And, yes, I was actually talking about an integration test (involving the browser), not a unit-test. Sorry for the sloppy use of the term.