JMockit: make mock class for Calendar or GregorianCalendar

K

killy971

I am using JUnit and JMockit to create test cases for my Java classes.
For some reasons, I now need to create a mock class for the java built-
in Calendar class (or GregorianCalendar), in order to test classes
which use the call "Calendar.getInstance()" and produce a result
specific to the current time.
The fact is that I have tried many ways to create this mock class, and
I didn't succeed in making it work properly (well, it completely
didn't work...).

I would like to know if creating a mock class for the Calendar or
GregorianCalendar is actually possible in java, and if some people
have already created it.
If you have some help to share about this problem, I would be pleased
to hear your advices.

Guillaume
 
O

Owen Jacobson

I am using JUnit and JMockit to create test cases for my Java classes.
For some reasons, I now need to create a mock class for the java built-
in Calendar class (or GregorianCalendar), in order to test classes
which use the call "Calendar.getInstance()" and produce a result
specific to the current time.
The fact is that I have tried many ways to create this mock class, and
I didn't succeed in making it work properly (well, it completely
didn't work...).

I would like to know if creating a mock class for the Calendar or
GregorianCalendar is actually possible in java, and if some people
have already created it.
If you have some help to share about this problem, I would be pleased
to hear your advices.

Guillaume

You cannot reasonably mock out static method calls -- doing so
involves monkeying with the classloader *before* the class on which
the call is being made is loaded, and the effects are global to the
application. This problem was so prevalent in one of my own apps that
I introduced a 'Clock' interface with one method (now()) that created
a Date (same story) representing the current time just so I could mock
out the clock for testing time-dependent code.

-o
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,816
Messages
2,569,714
Members
45,502
Latest member
Andres34P

Latest Threads

Top