Easymock Uses Date Pending

I am mocking a method with easymock that has a date in its body, something like this:

public void testedMethod() { ... if (doSomething(new Date())) { ... } 

And my test is as follows:

 public void testThatMethod() { ... expect(testedClass.testedMethod(new Date())).andReturn(false); ... } 

But when I run the test, sometimes I get this error:

Unexpected method call testedMethod (Thu Jan 28, 09:45:13 GMT-03: 00 2010): checkedMethod (Thu. 28 Jan. 09:45:13 GMT-03: 00 2010): expected: 1, relevant: 0

I think this is because sometimes the date has a slight difference. I tried some flexible expectations without success. Is there any way around this?

+4
source share
5 answers

Stop using the new date (), use a calendar with a constant time instead.

 //Declare the Calendar in your test method Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(0l); //receive the calendar to be used in testedClass constructor public void testedMethod() { ... if (doSomething(cal.getTime())) { ... } //use the same calendar to make the assertion public void testThatMethod() { ... expect(testedClass.(testedMethod(cal.getTime())).andReturn(false); ... } 
+3
source

We are constantly faced with similar problems, and these are the alternatives that I see:

  • Specify the date as a parameter for the (+) method Quick change (-) is a little dirty - when you just want to use "now", it also pollutes your interface
  • Pull the date from the QueryCurrentDateProvider employee (+) Still pretty fast (+) You can also scoff β†’ you are sure to use the same date (-) of unnecessary collaborators created for each service where you need to do something like this
  • Write your own EasyMock argument connector, where you abstract away what you really want to do - when you're just interested in the day, not the time when you can use something like commons DateUtils.isSameDay to start it (+) a clean solution (+) without changing your productive code (-) you should write your own helper (although I do not understand why EasyMock does not have this yet)
  • Move the β€œnew date ()” to a private method, then mock this method with something like PowerMock (+) quickly (+) a small change in the productive code (-) introduces power as a dependency
  • Change the Date parameter to String and use the general template to convert the date to a string before calling the method (+) quickly (+) no additional code, libraries required on the testing site (-) you need to format the date before calling the method and parse the date in the called method

So, it really suits you. When you work with current timestamps, I would recommend matching arguments - as this investment will quickly pay off.

+3
source

I just found this thread, and it helped me solve the problem for which I was stuck for a good hour.

I think that I would share my 2 cents:

If you don’t care about the date value and just want to know that it is a Date object, just use the predefined EasyMock connector:

 EasyMock.expect(objectMock.isPollingTimeOut(EasyMock.eq(600000L), EasyMock.isA(Date.class), EasyMock.eq(someMock))).andReturn(false); 

Remember, once you use the helper, you must use matches for all arguments in the method you are testing, for example, what I did.

+3
source

If you understand exactly why this fails, you can write your own interlocutor to be more flexible in comparing dates. See the Helper Section http://easymock.org/EasyMock2_2_Documentation.html

+2
source

Perhaps the millisecond portion of the dates is different. You probably need to use Calendar.set() from scratch before creating the date object:

 Calendar myCalendar = Calendar.getInstance(); myCalendar.set(Calendar.MILLISECOND, 0); Date testDate = myCalendar.getTime(); 

But this is an assumption :)

+2
source

Source: https://habr.com/ru/post/1299652/


All Articles