This week's book giveaway is in the Cloud forum.
We're giving away four copies of Terraform in Action and have Scott Winkler on-line!
See this thread for details.
Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

How to create a stream of objects inside a collectio of yet another collection ?

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I have the following code that creates an array of three ojbects repeatedly and saves them into an array list, and I like to return from the method a Stream of these three objects (KdTree, RectHV, and Point2D[]) yes the last one is yet another array, but it matches the unit test method signature. I first like to know please if there is a better way of doing this, and then how I can return the stream of my original objects because right now Junit5 gives me the following error:


Here is my entire test class:
 
Marshal
Posts: 74387
334
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not quite sure I understand the problem, but have you considered the flatMap() method?
 
Saloon Keeper
Posts: 13410
300
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JUnit 5 has an ArgumentsProvider interface that you can implement to provide arguments to a @ParameterizedTest. The KdTreeArgumentsProvider class implements this interface and is bound to the range() unit test using the @ArgumentsSource annotation.

The problem appears to be that JUnit can't seem to bind the values of the kt, r and expectedPoints variables on line 30 to the parameters with the same name on line 38. I'm not sure what causes the problem.

Sam, if you can clean up the code, we can see if we can reproduce the problem. Pay attention to the following things:

  • Don't use File to access application resources. Instead, use Class.getResourceAsStream().
  • Test resources should be in src/test/resources, not src/main/resources.
  • Get rid of the instance fields of KdTreeArgumentsProvider. You can use local variables instead.
  • instanceData and l are unused. Remove them.
  •  
    Sam Muller
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Good Morning and thank you for both replies,
    I wanted to post the version that is now working better, but the only problem I have is that it only runs the last instance of the tests. The entire project is here. It is Maven project in Intellij. Now I see all the Objects in the array at the end of provideArguments() method, and the test runs but it only runs for the last batch. I also wanted to say that I did try flatMap() but I believe I ran into an issue of Object not supporting it. But I will have a look again. Given that there are a number of items in your last advice I did not try yet, if you feel I should give them a try, please just say something like "try it" or something, but regardless I will. The other item I wanted to mention is why I created the two Point objects and the one rectangle and keep adding them to the data. They are place holders for now, and my goal is to have the flexibility to test the same KdTree with different number and kinds of rectangles, and the expected result will be different number of Points. And I wanted to ask please how to set up my input files and parse them. I think I am going to use a Pipe to separate the points data from rectangles and the expected points, but if you have a favorite link or something I can follow I would greatly appreciate it. I am also wondering if I should setup a small database. I have run mySql in the past.Here is what I have now:
     
    Sam Muller
    Greenhorn
    Posts: 12
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you so much for the advice. I have moved my test files to src/test/resources/ and I just viewed a tutorial on how Class.getResourceAsStream() works. Re: your advice on removal of the instances, I intend to and will do so. I intend to add more logic to build different instances for each test.
     
    Sam Muller
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I am also trying the getResourceAsStream() and it works as far as creating the tree I need, but I believe I still need an Object[] to pass my test data to the unit test method. So I think there was another reason you advised using this vs. File. Would you let me know please? Here is what I have:
     
    You showed up just in time for the waffles! And this tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic