• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can I test overloaded methods in JUnit test?

 
john sm
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

Survey.java


SurveyTest.java
 
Tomek Kaczanowski
author
Ranch Hand
Posts: 40
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic