• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

how to make a utility class and test class using Java

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I have the following class, our requirement is to divide this class to Utility and test Classes.
Our test code is getting optimized and wanted to make it more Utility and config driven.
Any thoughts, please ?

 
Marshal
Posts: 80874
506
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suggest you start from scratch with your testing class. I can see all sorts of problems with the current code. Some style and some logic problems:-
  • Always start ClassNames with CapitalLetters.
  • Class Names should be nouns; if you are using adjectives as class names, it suggests to me your class doesn't represent nor constitute an entity in the object‑oriented sense of the word.
  • Don&apo;t risk returning null in line 25: if you have a null throw an exception instead.
  • Only use @SuppressWarnings if you are sure the code it applies to is correct. The following method contains at least four significant errors..
  • Don't make your class do several things. You have methods to open the connection, to print a JSON and claiming to test something.
  • As you know, a utility class contains only static members, only private constructors to prevent instantiation, and no variables. Usually they contain only methods, one private constructor, and public or private constants. A utility class is intended to be used by other code rather than having an existence as an object in its own right.
    For unit testing you test every method available with “normal” and “incorrect” arguments. I would have thought you would be using a framework like JUnit.
     
    Arya Ramanujan
    Ranch Hand
    Posts: 40
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:I suggest you start from scratch with your testing class. I can see all sorts of problems with the current code. Some style and some logic problems:-

  • Always start ClassNames with CapitalLetters.
  • Class Names should be nouns; if you are using adjectives as class names, it suggests to me your class doesn't represent nor constitute an entity in the object‑oriented sense of the word.
  • Don&apo;t risk returning null in line 25: if you have a null throw an exception instead.
  • Only use @SuppressWarnings if you are sure the code it applies to is correct. The following method contains at least four significant errors..
  • Don't make your class do several things. You have methods to open the connection, to print a JSON and claiming to test something.
  • As you know, a utility class contains only static members, only private constructors to prevent instantiation, and no variables. Usually they contain only methods, one private constructor, and public or private constants. A utility class is intended to be used by other code rather than having an existence as an object in its own right.
    For unit testing you test every method available with “normal” and “incorrect” arguments. I would have thought you would be using a framework like JUnit.



    We are using TestNG in our framework. I have formed a utils class the following way :



    Test Class and tried the Test Class the following way :



    Using the utils object, why cannot I access the methods from the base class. Where am I going wrong ?
     
    Campbell Ritchie
    Marshal
    Posts: 80874
    506
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Why have you got two methods for opening and closing connections? I am not sure about Excel connections,  but if they are like database connections, they must be definitely closed before you close your app. That would usually be done with the try‑with‑resources construct. When you close the connection depends on how difficult it is to open a new connection, but I can envisage that method being used repeatedly and leaving unused connection objects lying around the heap until GC occurs.
    You have forgotten the private constructor.
     
    Sheriff
    Posts: 7126
    185
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Have you removed the package statements?  Please include them.  Is Utils in com\root\com?
     
    Rancher
    Posts: 89
    13
    Scala Eclipse IDE MySQL Database Tomcat Server Chrome Java Windows
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I know you asked about testing but your formatting is all over the place too.



    That block has all kinds of weird stuff going on. I know it might seem insignificant but when you are having others review your code, it matters for readability purposes and impressions (it's the same as mixing up they're, their, and there when you're inconsistent like that). CTRL+SHIFT+F is your friend if you're using Eclipse.

    Just for instance you have 3 different whitespace formats going on with your class/method names



    As far as tests code, You've already made this class, so unless you start from scratch you can't exactly implement TDD. Download a code coverage tool (never used that framework so I'm not sure if it comes with one), write logical tests for edge cases and normal cases for each method, run coverage tool, right tests to test what you haven't covered; logical ones, coverage for coverage's sake is bad. Rinse and repeat.

    -Zach
     
    Arya Ramanujan
    Ranch Hand
    Posts: 40
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Knute Snortum wrote:Have you removed the package statements?  Please include them.  Is Utils in com\root\com?



    I did not remove them but just while pasting, I deleted them, thought it may not be necessary.
     
    Arya Ramanujan
    Ranch Hand
    Posts: 40
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Zach Rode wrote:I know you asked about testing but your formatting is all over the place too.



    That block has all kinds of weird stuff going on. I know it might seem insignificant but when you are having others review your code, it matters for readability purposes and impressions (it's the same as mixing up they're, their, and there when you're inconsistent like that). CTRL+SHIFT+F is your friend if you're using Eclipse.

    Just for instance you have 3 different whitespace formats going on with your class/method names



    As far as tests code, You've already made this class, so unless you start from scratch you can't exactly implement TDD. Download a code coverage tool (never used that framework so I'm not sure if it comes with one), write logical tests for edge cases and normal cases for each method, run coverage tool, right tests to test what you haven't covered; logical ones, coverage for coverage's sake is bad. Rinse and repeat.

    -Zach



    Thank you - Crtl + Shift + F was very useful.

    "As far as tests code, You've already made this class, so unless you start from scratch you can't exactly implement TDD."
    I do not know how to start over, so I started with the Utils class, which is what was suggested. I will try using the coverage tool, any suggestions which tool works best for Eclipse.
     
    Arya Ramanujan
    Ranch Hand
    Posts: 40
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Knute Snortum wrote:Have you removed the package statements?  Please include them.  Is Utils in com\root\com?


    It is still "com.root.com".
     
    Knute Snortum
    Sheriff
    Posts: 7126
    185
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Arya Ramanujan wrote:

    Knute Snortum wrote:Have you removed the package statements?  Please include them.  Is Utils in com\root\com?



    I did not remove them but just while pasting, I deleted them, thought it may not be necessary.


    That's what I mean: in the future, please do include them when copying and pasting.  It helps us diagnose the problem.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic