Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Incredibly Confused About 'Test Code'

 
Matthew Bailey
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34672
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Vadim Konkov
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 136
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sudipto shekhar
Ranch Hand
Posts: 826
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And one more thing Welcome to Java Ranch.
 
Matthew Bailey
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic