• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

JMock Classes without a setter

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
author & internet detective
Posts: 42165
937
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 42165
937
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do you avoid the call in "test mode"?
 
Jeanne Boyarsky
author & internet detective
Posts: 42165
937
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
  • reply
      Bookmark Topic Watch Topic
    • New Topic