16

Wen I run my tests:

@RunWith(PowerMockRunner.class)
@PrepareForTest(MyStuff.class)
public class MyStuffTest { ..whatever

After I added ZonedDateTime class to that code it stared failing with the following error:

java.lang.IllegalStateException: Failed to transform class with name MyCode Reason: [source error] toInstant() not found in java.time.ZonedDateTime

Somewhere in my code I have:

long longTimeNoSee = ZonedDateTime.parse(getateTimeString()).toInstant().toEpochMilli();

I guess it is a bug in powermock. But maybe someone got some idea (?)

ses
  • 13,174
  • 31
  • 123
  • 226
  • 3
    Hello. Did you manage to handle this problem ? I have exactly the same situation with isBefore() method. – Thomas Mar 04 '15 at 08:58

4 Answers4

5

It seems to be a bug in Powermock indeed. See

https://github.com/jayway/powermock/issues/557

You might want to add your examples and vote for this issue.

UPDATE: According to Powermock it seems to be an issue in javassist: https://github.com/jboss-javassist/javassist/issues/43

Buurman
  • 1,914
  • 17
  • 26
  • I am seeing this error even though I am running PowerMock 1.6.5 which uses Javassist 3.20.0 so should include the fix to that issue. – Justin Emery May 25 '16 at 16:28
  • 1
    @JustinEmery i had the same issue. We use PowerMock 1.6.6 where the bug is marked as fixed. Our tests failed anyway. I did some research and it came out that our project libraries used an older version of javassist which was loaded precedent. So check if any of your libraries contain earlier javassist versions. Updating these jars fixed the issue for me. – DmiN Mar 15 '17 at 11:42
  • Upgrading `javassist` to `3.23.0-GA` and `powermock` to `1.7.4` works for me. – so61pi Dec 05 '19 at 05:31
3

Use powermockito version 1.6.6 or higher.

Also, make sure to use latest javassist version or at least 3.23.0-GA

Ashwani Kumar
  • 663
  • 3
  • 6
  • 18
  • The latest javassist version (currently 3.23.1-GA) doesn't work. Manually adding the javassist dependency for version 3.23.0-GA like you said fixed it for me. – Neets Aug 29 '18 at 15:13
2

I had the same issue. Fixed it by using Instant.from().

So in your case, below should work: ZonedDateTime.parse(Instant.from(getateTimeString())).toEpochMilli();

abdev
  • 597
  • 4
  • 17
Kamlesh Shewani
  • 329
  • 2
  • 2
0

NOTE: Strictly for people who tried everything in here and are still not able to get it running

I just moved the method containing using toInstant() to a new helper class.

And now everything works fine.

So here's what I suggest, you can make a new Helper/Util,Date_Helper class or move the relevant code to an already existing class. Now you can call the required functions from the class which you are trying to test.

No need to update dependencies. Just a tiny workaround.

abdev
  • 597
  • 4
  • 17