wood burning stoves 2.0*
The moose likes Testing and the fly likes How can I test overloaded methods in JUnit test? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "How can I test overloaded methods in JUnit test?" Watch "How can I test overloaded methods in JUnit test?" New topic

How can I test overloaded methods in JUnit test?

john sm

Joined: May 27, 2012
Posts: 1
Hello. I am trying to create a JUnit test of simple Getters and Setters but am unsure of how to test the overloaded testSurvey() and testSurveyLongString(). What can I do?
Also, in my Survey.java class where I have a List<Question> I want to instantiate an object if the instance variable is null but I get the error: ""Syntax error on tokens, Expression expected instead". What can I do to fix this?

Thank You


Tomek Kaczanowski
Ranch Hand

Joined: Oct 26, 2005
Posts: 40

Hi John,

what I would suggest, is to think about the "contract" your code has, and also about its breakable parts.

Take a look at this Survey class:

what things really deserve to be tested, what can a client expect from this class?

I would say that you should concentrate on the following:
a) after creation the isActive should return false (no matter which constructor was used - so you should probably write two tests here)
b) after creation the list of questions is empty (but not null!)
c) if shorter version of constructor was used, then the Survey should have ID = 0, and an empty title
d) if longer version of constructor was used, then Surver should have ID and titles as specified by parameters,

As for getters/setters it is not a bad thing that you try to test them, however one could argue that they are "too simple to break". I guess you haven't written them by hand but used some autogeneration feature of Eclipse (or another IDE). Thus the chances of invalid implementation are minimal. What I'm trying to say, is that you should concentrate on the responsibilities of the class, and NOT on single methods.

As for you question regarding how to test short and long constructors, I do not see what is the problem really. In one test method you create an object by using short constructor and then test its properties, and in another method you use longer constructor and also verify if it worked as expected. Not sure which part you are having troubles with.

Tomek Kaczanowski
Book author: Practical Unit Testing with TestNG and Mockito
It is sorta covered in the JavaRanch Style Guide.
subject: How can I test overloaded methods in JUnit test?
Similar Threads
Help needed on the following example
EJB3.0 example
jboss 6 and persistence.xml file
Junit Testcase Array problem
I want to use indexed properties