• 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
Marshal
Posts: 34839
369
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
Marshal
Posts: 34839
369
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
Marshal
Posts: 34839
369
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.
  •  
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic