File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Testing and the fly likes EasyMock-Verify return values of a private method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "EasyMock-Verify return values of a private method" Watch "EasyMock-Verify return values of a private method" New topic
Author

EasyMock-Verify return values of a private method

Shani Gunarathna
Greenhorn

Joined: Jul 07, 2009
Posts: 11
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

Joined: Jul 11, 2001
Posts: 14112
Uh, I think you misunderstood what EasyMock is good for.

Anyway, why do you need to "verify" the return value of the private method???


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Shani Gunarathna
Greenhorn

Joined: Jul 07, 2009
Posts: 11
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 29257
    
140

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Shani Gunarathna
Greenhorn

Joined: Jul 07, 2009
Posts: 11
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 29257
    
140

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

Joined: Jul 07, 2009
Posts: 11
Hi Jeanne,

Got your point. Thanks a lot.

regards,
Harshani
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EasyMock-Verify return values of a private method
 
Similar Threads
Testing dynamic data from the database
easymock: problems with custom Argument Matcher
<NaN> returned for my Junit test case
EasyMock
Unable to understand : No Last Call on a Mock available. _ Easy Mocks