aspose file tools*
The moose likes Testing and the fly likes testing private method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "testing private method" Watch "testing private method" New topic
Author

testing private method

Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1106

i have 2 methods:



so the only logic is the private mapper method, i have 3 options for testing this which is better
1) make the method protected
2) make it a mapper class
3) spy on the variables sent to the diaryService.update method.

i suppose there is option 4, refactor the code to remove this whole problem, but I have no idea how that could be done.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

how about an update entry builder with a buildFrom(DiaryEntry) method? You can test that pretty cleanly. I would start with a builder class that is package private in scope too.


Junilu - [How to Ask Questions] [How to Answer Questions]
Geroen Joris
Greenhorn

Joined: Feb 20, 2013
Posts: 4
Well, I guess there's also option 5:
- using PowerMockito, mock the constructor calls
- check the calls on the mocks
- call the private method via reflection

Option 6 comes to mind too: make the method package local, and access it that way in your test (given that your test is in the same package)
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

I recommend Junilu answer, remember if you're mocking private methods, there is something wrong with your design.

Secondly, if you're already stuck in legacy code base or don't want to touch/alter current code, try Mockito Argument Matcher on m_diaryService.update(update);, if you can mock m_diaryService.


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Geroen Joris
Greenhorn

Joined: Feb 20, 2013
Posts: 4
I'm not talking about mocking, I'm talking about accessing. And I have been in the same situation - after having used "extract method" enough in your IDE to separate the different levels of abstraction.

I do, however, tend to agree with you in this case. The builder is the better option. The private method doesn't seem to contains something that's to be considered private to this class.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: testing private method
 
Similar Threads
annotations error
Issue while deploying web service jwstutorial example CLI171
Appending to a Blob
A Simple Test Framework using JUnit and Spring (Part 1)
Help in understanding the add method particularly addBefore of linkedList Implementation