jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes Incredibly Confused About 'Test Code' Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Incredibly Confused About Watch "Incredibly Confused About New topic
Author

Incredibly Confused About 'Test Code'

Matthew Bailey
Greenhorn

Joined: Jan 29, 2009
Posts: 6
Hello all.

I am reading through "Head First Java" to help me with my studies in college and the book mentions a test class that includes the main method. The book says that the test class is apparently used to make your objects and then, it seems, it contains the code to actually run your program. Why is it called a test class? How is it testing methods when what you are doing in the tester class is the actual thing that you want the program to do?? The book has just completely confused me, could somebody here please try and explain the 'test class', what it is used for and why. I really don't undertsand the concept.

Thank you
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30758
    
156

Matthew,
In a larger program, you want to test smaller pieces of a program than "the whole thing." Suppose I have a Television class. I want to know that works before writing other classes that depend on it. I'll write a separate test class/driver program to make sure it works. (I'll actually use the JUnit library, but that's more advanced.)


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Vadim Konkov
Greenhorn

Joined: Sep 30, 2008
Posts: 8
You can also test out your methods by creating an instance in the main method and calling the other methods like that. I feel that this way will possibly save time and provide and alternative for someone who is brand new to java and only writing simpler programs.

Sorry for the long example but it may have caused confusion if I just showed you the main method.

Balagopal Kannampallil
Ranch Hand

Joined: Oct 18, 2004
Posts: 136

Don't get confused Matthew. You can define a test class as a class which simulates the things which are expected to happen when the real code is up and running and the people are using you code.

Take an example you have a MobilePhone class which has two attributes model and color.
In another class PriceFetcher you have a method which accepts a MobilePhone object and returns its price. Something like this.

In a real scenario, you might be having this code in a web application where the user selects the color and model in the browser and clicks a "Find Price" button. The application runs in the background, do all the database calls and the price is shown to the user.

You want to test this application now. Will you do the tedious job of deploying your web application in you server and try to simulate the things which a user is trying to do? No...

Here is where the test class comes into play. You want to now test the public String getPrice(MobilePhone mob) method. But it needs a mobile phone right? No problem, I will create a MobilePhone object (we can call it a dummy phone) and pass it to this method. You will do something like this in your test method.

The getPrice() method will think, wow I just got a mobile phone object (it is not worried whether it is a dummy phone or an original one), I should returns its price. You can print the price and see whether it is the correct price for the mobile phone which you just gave. Now, you can make sure whether your code is running fine or not.

SCJP 5.0 and now fighting with ExtJS
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 823

And one more thing Welcome to Java Ranch.


Regards, Sud.
SCJP 5 ScjpFAQ JLS
Matthew Bailey
Greenhorn

Joined: Jan 29, 2009
Posts: 6
Thanks for the help everyone. I think I understand it a little better now. So I include the test class to try out the methods of the other classes and to see that they will work, right?
 
wood burning stoves
 
subject: Incredibly Confused About 'Test Code'