• Post Reply Bookmark Topic Watch Topic
  • New Topic

Testing Class Java  RSS feed

 
R L Miller
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • 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




 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • 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.
 
Knute Snortum
Sheriff
Posts: 4079
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
Kim Cook
Greenhorn
Posts: 5
Android Java Python
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • 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
  • 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: 4079
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • 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?
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!