This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Testing and the fly likes Junit testing with dependencies Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Junit testing with dependencies" Watch "Junit testing with dependencies" New topic
Author

Junit testing with dependencies

Pradeep Kumar
Ranch Hand

Joined: Sep 08, 2006
Posts: 108
Hi friends,
I am a beginner in Junit testing and just started exploring Junit for writing the test cases. I have a scenario where I am struck and finding it difficult to proceed. The scenario is that I am testing a public method which internally calls private methods. In on of the private method, the method tries to connect to the environment and the properties of the environment is fetched in the private method. How can i override the behavior such that I can override the behavior by populating the properties and mimic the environment dependency even though I do not directly call the private method.

Ex:

In the main class:

In the ConfigDetail class



Please let me know to do proceed.
Thanks and Regards,
Pradeep


Thanks and Regards, Pradeep Kumar
SCJP 1.6, SCWCD 5.0
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30068
    
149

Pradeep,
You are going to need to refactor that code to make it testable. Ideas:
  • 1) Pass in ConfigDetails so you can make your own - only works if you want to integration test thru to the database
  • 2) Move the private method code to an other class with an interface and mock it out
  • 3) Make the private method default access or protected and override it to do nothing.


  • #3 is often a hack, but it is easiest to do. Especially if you can't change the code much.


    [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
    Pradeep Kumar
    Ranch Hand

    Joined: Sep 08, 2006
    Posts: 108
    Jeanne Boyarsky wrote:Pradeep,
    You are going to need to refactor that code to make it testable. Ideas:
  • 3) Make the private method default access or protected and override it to do nothing.


  • #3 is often a hack, but it is easiest to do. Especially if you can't change the code much.


    I like the idea of the 3rd option. How do i override the method and test the method. Do you mean extends the class and override the method and test the sub class?
    Jeanne Boyarsky
    internet detective
    Marshal

    Joined: May 26, 2003
    Posts: 30068
        
    149

    Pradeep Kumar wrote:
    Jeanne Boyarsky wrote:
  • 3) Make the private method default access or protected and override it to do nothing.


  • #3 is often a hack, but it is easiest to do. Especially if you can't change the code much.


    I like the idea of the 3rd option. How do i override the method and test the method. Do you mean extends the class and override the method and test the sub class?

    Exactly!

     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Junit testing with dependencies
     
    Similar Threads
    How to test private method in JUnit?
    EasyMock-Verify return values of a private method
    Application Testing Techniques
    A Simple Test Framework using JUnit and Spring (Part 1)
    My first interface