0

I have a class as follows:

public class MyClass{

  Connector con;

  public MyClass(Connector con){
     this.con= con;
  }

  public void save(Xyz xyz){
     //save 2 instances of xyz one with lastupdatetime as 0 and other with 
    // currenttimestamp
     xyz.setLastUpdateTime(0) ; a
     con.save(xyz) ;
     xyz.setLastUpdateTime(Calender.getInstance().getCurrentTimeInMillis() );

     con.save(xyz);   
  }

}

How can i write test case of it using easymock.

The problem is that the time stamp is found by the method at runtime. And its different from one in mocked object.Can i ignore specific param of Xyz class

What can i specify to ignore specific attribute while mocking?

 Easymock.expect(con.save(xyz)).andReturn(something) ??
user93796
  • 18,749
  • 31
  • 94
  • 150

1 Answers1

0

A few things you could do, in rough order of which I'd do if I had the choice.

  1. Create or find an existing Clock class that wraps the Calendar call. You can then provide a mocked Clock that returns a user defined time, and is thus testable. This is fairly common, and it's easier from a testability standpoint if you're not relying on static methods directly.

  2. Get the time from a getCurrentTime method on the class, which you can then override.

  3. Write a custom matcher. Example: http://toddscodenotes.blogspot.com/2009/09/creating-easy-mock-custom-matcher.html.

  4. Use EasyMock.capture() to capture the call instead. Then you can inspect the fields from the capture object.

  5. Look into extensions to EasyMock (PowerMock?) which have the ability to mok statics. Not sure if that would even apply here since you're not mocking Calendar.

James
  • 8,512
  • 1
  • 26
  • 28