• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Where to create object in this code for testing it?  RSS feed

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to test the following class. I have created it in NetBeans. Then I used the Tools->Create update Test



Do I have to provide the main method & write the following two bold faced statements in the main method? Kindly guide me where to create the object? I have to write following two statements:



The test class code is given below:



Some body please guide me.

Zulfi.
 
Bartender
Posts: 9494
184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what you're asking. How to run the test?

You can just right-click your project in Netbeans and then select "Test".
 
Zulfi Khan
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks for  your reply. I have not created an object of  the class which I am testing. Where should i create its object? The name of the class which I am testing is PrintGrades. I dont have any'main' method in this class. Should I provide a main method and then create its object in the main and then call the method print(...), using the following code


Is the main method necessary in the class which I have to test?
Please guide me.

Zulfi.
 
Zulfi Khan
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am getting the message that test has failed. Somebody please guide me why the test failed?
The code of the class which I am testing is:


My tester method's code is:


Somebody please guide me.

Zulfi.
 
Rancher
Posts: 3757
40
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the exact output of your test run?

I would expect it to fail as the last line of the test is a call to fail.
 
Stephan van Hulst
Bartender
Posts: 9494
184
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First off, use the recommended naming conventions and use strong types over string. You will also want to declare your methods as static if they don't need a reference to instance fields. Don't declare variables if you're not going to use them. You're not dong anything useful with strGrade.

The final result would become something like this:

Your test method becomes something like this:

You don't need to create an object, because the method is static. Your current test fails because you explicitly let it fail by calling fail().
 
Zulfi Khan
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks my friends. You have solved my problem. I have used following condition:


It really helped me.

Now I have question on your code.


The above does not look like a java code. It seems as if 'Grade' is the class name. But then what are Grade.A, Grade.B, Grade.C? No values are assigned to them

Where I have to put the above method ? Inside the Junit class?

Thanks for your time.

God bless you.

Zulfi.

 
Marshal
Posts: 61766
193
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Zulfi Khan wrote:. . . The above does not look like a java code.

It is perfectly good Java┬« code. It does however show multiple returns, which some people are happy with and some not. Please tell us what the mark to get a grade A is.

It seems as if 'Grade' is the class name.

Yes, Grade is the class name to be returned.

But then what are Grade.A, Grade.B, Grade.C?  . . .

They are constants in the enumerated type Grade.
 
Dave Tolls
Rancher
Posts: 3757
40
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Zulfi Khan wrote:Hi,
Thanks my friends. You have solved my problem. I have used following condition:




In your original code above you already had:

which is what you ought to be using to test those values.
fail should only be used to indicate a test that hasn't been written yet (IMO).
 
Stephan van Hulst
Bartender
Posts: 9494
184
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like Dave says, you don't need the condition with the fail() statement. The assertEquals() statement already fails the test if the return value isn't the expected value.

I would move more of the business logic to the enum that I used in my last example. It would look like this:
 
Zulfi Khan
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Many thanks my dear friends for your cooperation. I like your posts. I am going to modify my code. I am going to call my test class in the main method using


I have added the jar files. Post again if faced problems.

Thanks for enum class. Its really a nice code.

Zulfi.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!