| Author |
JMock Classes without a setter
|
Tim Holmes
Greenhorn
Joined: Aug 26, 2005
Posts: 25
|
|
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
internet detective
Marshal
Joined: May 26, 2003
Posts: 26218
|
|
|
Refactor! Seriously, what are you trying to inject. The string "test" or something else?
|
[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
|
 |
Tim Holmes
Greenhorn
Joined: Aug 26, 2005
Posts: 25
|
|
|
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
internet detective
Marshal
Joined: May 26, 2003
Posts: 26218
|
|
|
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
Joined: Aug 26, 2005
Posts: 25
|
|
|
How do you avoid the call in "test mode"?
|
 |
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 26218
|
|
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.
|
 |
 |
|
|
subject: JMock Classes without a setter
|
|
|