• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Some questions arising from my first JUnit test case

 
Ranch Hand
Posts: 254
1
MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have started writing unit tests. I chose a class which has three static methods for the purpose of testing.

I have as a business case where I have a method to construct a string as per custom object -- making use of its properties/fields in the message.

The custom objects .java file is as below:




My JUnit test case is based on the following:



This means, MyTestClass.constructMsg(cust) is what I want to test. sampleMessage is message created on the fly declared as static.

I would like to know; is the constructing of object in the setUp() rightly done? Do you think the assertEquals() be used on strings?

Additionally, can I add other methods of the class under test below this test method? Although this test passes is it enough for me -- as a programmer -- to be confident of my method.

 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unless your development organization is stuck using Java 1.4, you should move to JUnit 4 which does not require you to extend TestCase.

In answer to your questions:

1. setup() is used to initialize test fixtures and preconditions that are common to all tests. If the cust object is going be common to all tests, then what you did is fine.
2. assertEquals() can be used with pretty much anything, including Strings.
3. Yes, you can add more test methods.

For effective use of unit tests, I recommend you read up on the practice of Test-Driven Development.

For me, unit tests will become part of the detailed design specification, providing concrete examples of how you should expect classes to behave under different conditions. Unit tests should be FIRST - Fast, Independent, Repeatable, Self-verifying, and Timely. Unit tests should help you drive design thinking, help you make better design decisions, as well help you refactor your designs safely. Understanding these things before you move on down the path you're heading now will hopefully make you rethink your approach to using and writing unit tests.
 
Ahsan Bagwan
Ranch Hand
Posts: 254
1
MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will keep these points in mind, thanks!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic