This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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.)
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.
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.