• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Use DAO stub for unit testing

 
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class that fetch credentials from a database and return. I need to write a unit test for this. I created a DAOStub to use, but I am not able to understand how to use this stub in my test class. Please suggest.

Example.java

ExampleTest.java

 
author & internet detective
Posts: 40211
816
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll need to refactor Example so that is is testable. To do this:
  • Make account an instance variable
  • Add another constructor that takes a String and IServiceAccount
  • Have your existing constructor call the new one with the account instance you use now from the factory
  • Have your test use the new constructor



  • This technique is called dependency injection.
     
    swathi bairu
    Ranch Hand
    Posts: 85
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I made the following changes as you said:

    Changed account to an instance variable -- but unable pass an instance variable to the constructor call
    Added another constructor that takes string and IServiceAccount
    Made my existing constructor call the new one

    But my class is not working as expected with these changes.

    Example.java


    Please suggest.
     
    swathi bairu
    Ranch Hand
    Posts: 85
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I made the following changes to pass account to the Test class. But this class is not giving expected result. Please suggest.

    Example.java
     
    Jeanne Boyarsky
    author & internet detective
    Posts: 40211
    816
    Eclipse IDE VI Editor Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    In your previous post

    should be


    That way you get the real object in the constructor for the real code. And the test can pass in the stub.
     
    swathi bairu
    Ranch Hand
    Posts: 85
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That worked. Thank you.
     
    I am not young enough to know everything. - Oscar Wilde This tiny ad thinks it knows more than Oscar:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic