• 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
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Testing Class Java

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, So I am needing to create a class specifically for testing the code below. My IDE is not allowing me(probably for good reason) to create an additional class in this package for testing. In previous classes we just did our testing in our main method so this is completely new to me.
How do I go about creating a test class to test my methods in the code below. Everything I have found online about testing requires plugins or additional software. Although, probably not relevant , I am using Netbeans. I appreciate any help I can get. N




 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

How do I go about creating a test class to test my methods in the code below.  


I'm not sure what you are asking.  A class is created by entering source code into a file and then compiling it.
If the purpose of the class is to test the methods of another class, the test class would create instances of the class using its constructors and then use those instances to call the classes methods.  There would need to be special code in the test class to check the results of the calls to the methods to see if the methods preformed as desired.
 
Sheriff
Posts: 7125
184
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
Are you asking about creating a test program with something like JUnit?

There are lots of ways to test your classes.  JUnit is used for something called unit testing.  

Generally unit tests are in a different path than the program being tested, but still in the same package.  That might seem confusing without an example.  So say I have a program in

workspace/MyApp/src/main/java/net/snortum/myapp/MyClass.java

"workspace/MyApp" is your project directory.  "src/main/java" is one convention for ordering Java files.  "net/snortum/myapp" is the package and MyClass.java is the program to test.  Your testing program might go in

workspace/MyApp/src/test/java/net/snortum/myapp/MyClassTest.java

Notice that it is in a different path, but in the same package.
 
Greenhorn
Posts: 5
Android Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi RL, I found a tutorial in the link below on how to write JUnit tests in Netbeans that might be helpful.  Hopes this helps.

https://netbeans.org/kb/docs/java/junit-intro.html
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That tutorial looks promising. And welcome to the Ranch
 
R L Miller
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
    Thank you for all the replies. I will read through that document shortly. I Guess my question was vague and probably symptomatic of my larger lack of understanding. I don't really understand file maintenance in Java.  This makes me pretty useless from a developer standpoint when I don't know what code should be in which file. Based upon what I can infer from Netbeans, I cannot call on and instance of a class from a separate file unless that class is made public. Despite the fact that my parent class is declared public, my IDE throws me an error that my children are not public and cannot be accessed from outside of the file. In order to make my child classes public, I would need to create a separate java file for each of the children to which, I suppose, I import the parent class(?) somehow.  Now, I guess instead on having 1 _Package_.java file containing all of my classes I am forced to create 4 separate java files and somehow reference them to each other. I apologize for posting such a rudimentary question but I have been, thus far, unsuccessful in my google searching.

    Can someone provide me a real workplace example of how the files would be developed in a working environment. We can use my simple Account, Saving, Checking, Test Account as an example or any other simple example someone may have. So in the real world, would I create the parent and child classes in one .java file and then use a separate for testing or would I create a separate file for each.

I have uploaded a picture of my file tree in Netbeans so you can see where I currently have my two files stored.


Java.PNG
[Thumbnail for Java.PNG]
Netbean File Tree
 
Knute Snortum
Sheriff
Posts: 7125
184
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

R L Miller wrote:I apologize for posting such a rudimentary question but I have been, thus far, unsuccessful in my google searching.


There's NoNeedToSaySorry.  This is exactly the place to post rudimentary questions.

Despite the fact that my parent class is declared public, my IDE throws me an error that my children are not public and cannot be accessed from outside of the file.


If you're talking about parent and child classes, are you extending them?  (A more correct phrase would be super class and subclass.)

What is the full text of the error?
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The base you should work from (and there are exceptions, but you;re just starting) is to stick each class in its own file.
If nothing else it helps you to view them as entities in their own right.
It's also sort of what Java was designed around.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic