Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EasyMock-Verify return values of a private method

 
Shani Gunarathna
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am new to EasyMock and i am witting a test case for a public method(return type is void) which access a private method.
I couldn't find a way to verify the return value of that private method. Can somebody please be kind enogh to explain it.

Thanks and regards,
Punnadi

say for an example;

public void test(){
//some coding here
value("qweqwty");
}

private String value(String s){
//some coding here
return stringVal;
}
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uh, I think you misunderstood what EasyMock is good for.

Anyway, why do you need to "verify" the return value of the private method???
 
Shani Gunarathna
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually the requirement is to verify the return value of the private method. For an example, say i am trying to remove unwanted characters
from a string via this private method. So how am i suppose to verify this behavior. Since the method is private i can't directly use assertEqual().
Kindly help me please.

Thanks in advance,
Harshani
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34401
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show the methods you are trying to test? In your example, the private method and test are in the same class - which I'm sure isn't the case in your code.

Basically the choices are:
- make the method package private or public and test it that way
- test through the public API.

Mocks won't help you here.
 
Shani Gunarathna
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
I guess my method name confused you. These two methods are in a same class.

class Abc{

public void test(){ <---- This is the method that i am testing with Junit
//some coding here
value("qweqwty");
}

private String value(String s){
//some coding here
return stringVal;
}

}

Let me tell you the actual requirement.
I am trying to write a filter in order to secure URLs from injecting malicious code in to it. I am using that private method to remove unsafe characters.
Now i wanted to test the code. private method get called by the public method as in the above code. How am i suppose to check the return value of the private method. Whether it is correct or not?
That means how am i going to verify whether all the unsafe characters are removed by that method or not? Hope you understand this more clearly now.

Thanks in advance,
Harshani
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34401
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harshani,
That makes more sense. I would make an extra public method. You already have the doFilter() method, which you understand isn't the simplest thing to test. And you have a method like "boolean checkForUnsafeCharacters". There's nothing to stop you from having this helper method be public (or default package private) - making it easy to test.
 
Shani Gunarathna
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeanne,

Got your point. Thanks a lot.

regards,
Harshani
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Speaking more generally, I see three fundamental solutions for such a problem:

- the functionality is simple enough that the private method can be tested indirectly through the public API.
- making the method public doesn't bloat the class' interface with inkongruent behavior. That is, making the method public makes sense for the class.
- move the method to a different (new?) class where the method is naturally public.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic