Thanks for the feedback I will try that out. I appreciate the time
you took with your response as well because I realize what I am doing
is pretty far out there.
Here is why I like powermock for this solution:
If powermock can work this way then you have the ability to mock out
anything that you need to in a running system.
In addition, when you use apectj I only want that behavior when we run
integration tests, so now I have to control that additional build
configuration.
With powermock all I have to do is set a flag in a file that I read in
which essentially tells me go generate the necessary mock
implementations. Specifically change Calendar.getInstance() and new
Date() to return the value that I tell them too....who said that time
travel isn't possible :)
I agree that aspectj is an option but IF powermock could do this IT
WOULD BE HUGE FOR ME!
Thanks Again,
Brayn
On May 3, 12:55 am, Johan Haleby <
johan.hal...@gmail.com> wrote:
> Hi Bryan,
>
> I understand what you mean but I don't think that PowerMock is the best tool
> for this. I would probably use something like Aspectj myself. How ever it
> may be possible and it's quite interesting technically so perhaps we should
> trying adding some better support for it in the future. And mocking out
> System.currentTimeMillis() will be even trickier because it's a java system
> class<
http://blog.jayway.com/2009/05/17/mocking-static-methods-in-java-syst...>.
> I released a new version of PowerMock yesterday that uses a Java agent to
> bootstrap and you can possibly try it out if you like but it won't be
> super-pretty. If possible I would recommend that you wrap
> System.currentTimeMillis() in your own static method that you stub with
> PowerMock. Otherwise go for Aspectj!
>
> 1. Follow the instructions on
> this<
http://code.google.com/p/powermock/wiki/PowerMockAgent>page to
> get the agent into the classpath.
> 2. Initialize the Agent manually, possibly in a static constructor:
> PowerMock.initializeIfNeeded();
> 3. PowerMockAgentTestInitializer.initialize(target.getClass());
> 4. Stub the method using the MemberModification API in powermock-support.
>
> Something like:
>
> @PrepareForTest(SomeClass.class)
> public class MyMethodStubber {
>
> public void stubMyMethodToReturnSomeValue() {
> PowerMockAgent.initializeIfNeeded();
> PowerMockAgentTestInitializer.initialize(
> MyMethodStubber.class);
> stub(method(SomeClass.class, "myMethod")).toReturn("some
> value");
> }
>
> }
>
> But for system classes this will be harder since you need to prepare all
> classes that interact with System.currentTimeMillis instead (refer to the
> blog).
>
> Regards,
> /Johan
>