• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
Bartenders:
  • Himai Minh
  • Carey Brown
  • Frits Walraven

what is the best way of unit testing a private class

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I am using Junit and since my project is a personal one, I tried to change the private class to public and write tests for it. But Intellij compiler complained saying: "KdTree is not an enclosing class" KdTree is the class that holds my other class called IntervalST. Here is the line I am trying to put in: To their / its credit Intellij does offer to change my code to a static class, and the error goes away. I was just wondering why I get the error because I know KdTree encloses my other class, and the code compiled fine earlier. Here is the entire project: kd Thanks
 
Marshal
Posts: 73322
332
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you have written the code as if Interval<T, U> were a public static nested class in KdTree. If you want to instantiate it from outwith its enclosing type, you would have to write something like this, which means you need a second object (=instance of the encloding type), and the instance you get implicitly has a KdTree.this reference somewhere. Both of which problems explain why ItelliJ is right to suggest you make that nested class static.I shall add you to our tests forum.
By the way, does Interval need two format type parameters if they are both going to point to the same type?

[additional]Sorry for spelling the types differently from your spelling.
 
Sam Muller
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Both of which problems explain why ItelliJ is right to suggest you make that nested class static.I shall add you to our tests forum.
By the way, does Interval need two format type parameters if they are both going to point to the same type?

[additional]Sorry for spelling the types differently from your spelling.


Thank you so much for the response. Intellij has been amazing in helping me ramp up, and learn more. And you are so right to point out the redundant keys. I will change it. I am struggling with learning the data structures and what they can do, and how to best implement the specifications. Yesterday I was only focused on compiling the code, and today I am working on testing and getting the functionality I want. Thank you so much for the quick and helpful response. No worries on spelling:-)
 
Campbell Ritchie
Marshal
Posts: 73322
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sam Muller wrote:. . . Thank you so much . . .

That's a pleasure

redundant keys. . . .

Keys? I think you mean type parameters. The jargon might be totally incomprehensible to people not working in our field, but it is very precise and expressive to us. We must however use it correctly.

Yesterday I was only focused on compiling the code, and today I am working on testing and getting the functionality I want. . . .

That sounds like a way of making lots of additional (or unnecessary) work for yourself. Plan what fundtionality you want. Write down how you think you are going to achieve it. Only then should you start writing any code. Once you get into that sort of discipline you will actually find the code quicker to write.
 
Marshal
Posts: 16454
272
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sam Muller wrote:I am struggling with learning the data structures and what they can do, and how to best implement the specifications. Yesterday I was only focused on compiling the code, and today I am working on testing and getting the functionality I want.


What you describe there is called the "test after" approach. This approach tends to produce untestable or difficult-to-test designs, designs that are only testable from the UI or outermost layer of the program. It's often the case that little bits of functionality that are found deeper in the design is difficult to test without a lot of setup.

On the other hand, a test-first approach, where you use tests to drive your design decisions, express intent, and give examples of how you want your classes are to be used tend to drive designs that are more loosely coupled and easier to unit test. If done with the discipline of refactoring, you can produce code that is easier to understand and change in the future.

Show me code that was written tests-after and some code that was written test-first and I'll show you the difference between code that's a pain to work with and code that's a joy to work with.
 
Sam Muller
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do not know how to thank you. I'll revise my strategy and proceed.
 
reply
    Bookmark Topic Watch Topic
  • New Topic