Java Mailing List Archive

http://www.gg3721.com/

Home » user.jmock »

[jmock-user] Ignoring calls to static methods

Panos Konstantinidis

2010-02-09

Replies: Find Java Web Hosting

Author LoginPost Reply
Hello my name is Panos, I am new to the list (although not to jmock since I've been using it for the last coupel of years).

I came across a situation that I need to re-use some legacy code that has loads of calls to static methods. For example consider the following example

class A
{
   B.instance().sayHello();
}

class B
{
   public static instance()
   {
      ...
      return B;
   }

   public String sayHello()
   {
      return "hello";
   }
}

I am testing class A with jMock. Is there a way to instruct jMock to completely ignore the call to B.instance().sayHello();? Normally this wouldn't be a problem but the call to instance() returns null sometimes and subsequently throws a npe.

I know that I could refactor the code and use the Strategy pattern but refactoring it is out of the question, I cannot touch the legacy code.

The other thing I can think of is to catch the npe and continue normally but this is not a good programming practice. So I am looking for a way (if there is any) to just ignore the static call. Any help is appreciated.

Regards

Panos


©2008 gg3721.com - Jax Systems, LLC, U.S.A.