This week's book giveaway is in the HTML/CSS/JavaScript forum.
We're giving away four copies of Practical SVG and have Chris Coyier on-line!
See this thread for details.
Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JMock Classes without a setter

 
Tim Holmes
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,

I am using JMock and my class under test calls certain other classes. The classes that are called are not injected into my class under test. I know how to inject mocked objects with a setter, but what do I do for classes under test that call other classes and my class doesn't have a setter to inject that mocked object?

 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 35762
412
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Refactor! Seriously, what are you trying to inject. The string "test" or something else?
 
Tim Holmes
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Um I don't think you understood the question. I am not trying to inject anything. I am trying to mock DOMParser. This is just an example.
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 35762
412
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't mock the Dom parser. You can avoid the call in test mode. Or wrap it in a class/interface you can mock.
 
Tim Holmes
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you avoid the call in "test mode"?
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 35762
412
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holmes wrote:How do you avoid the call in "test mode"?

It's hard to recommend without seeing the surrounding code. The most common ways are:
  • Put the code you don't want to call in a protected method. Create another class that subclasses the real class and overrides that method to do nothing. (or set values/simulate a call/etc)
  • Use a boolean flag to control whether a segment of code is run. This can be externalized to a property file or environment setting for a test mode.
  •  
    Skool. Stay in. Smartness. Tiny ad:
    the new thread boost feature: great for the advertiser and smooth for the coderanch user
    https://coderanch.com/t/674455/Thread-Boost-feature
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!