Powermock in play 1.2.4

839 views
Skip to first unread message

aviandri hidayat

unread,
Dec 12, 2011, 11:55:42 PM12/12/11
to play-fr...@googlegroups.com
Hi all,


I've been wondering the best practice of implementing unit test in
play. Looking at how the injection from spring ioc or guice works,
where it injects dependency classes to static field, I belief I would
be needing powermock to mock all those static classes.
The problem is I just couldn't make powermock to run.

Here is my dependency.yml :
- play
- play -> db 1.1.1
- play -> mockito 0.1
- net.htmlparser.jericho -> jericho-html 3.2:
transitive: false
- org.powermock -> powermock-module-junit4 1.4.10
- org.powermock -> powermock-api-mockito 1.4.10:
exclude:
- org.mockito -> mockito-all


When using the powermock in the tests I got this exception :

java.lang.ClassNotFoundException: utils.photograbber.TestServiceTest
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:143)
at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:65)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)


Anyone can help? Or is there any best practice to do unit test (isolated tests)

regards.
Aviandri

Grzegorz Słowikowski

unread,
Dec 13, 2011, 2:51:30 AM12/13/11
to play-fr...@googlegroups.com, aviandri hidayat
Hi

Can you send me sample project?

Grzegorz Slowikowski

aviandri hidayat

unread,
Dec 14, 2011, 4:53:32 AM12/14/11
to play-fr...@googlegroups.com
Hi,
I've made a very simple project that shows how I setup and
usepowermock in playframework
http://dl.dropbox.com/u/1096256/sampleapp.zip

Thx
2011/12/13 Grzegorz Słowikowski <gslowi...@gmail.com>:

mszalinski

unread,
Jan 22, 2012, 11:48:55 AM1/22/12
to play-fr...@googlegroups.com
Have anyone found solution or workaround to use Powermock for testing inside PlayFramework applications?
Standard using of both PowerMockRunner and PowerMockRule leads to ClassNotFoundException on test classes :(

Thanks,
Michal

mszalinski

unread,
Jan 21, 2012, 10:16:05 AM1/21/12
to play-framework
I've exactly the same problem,
using play 1.2.4
- play -> mockito 0.1
- org.powermock -> powermock-module-junit4 1.4.11
- org.powermock -> powermock-api-mockito 1.4.11

Have anyone found any solution for the problem?

Regards
Michal

On Dec 14 2011, 10:53 am, aviandri hidayat <avian...@gmail.com> wrote:
> Hi,
> I've made a very simple project that shows how I setup and
> usepowermock in playframeworkhttp://dl.dropbox.com/u/1096256/sampleapp.zip
>
> Thx
> 2011/12/13 Grzegorz Słowikowski <gslowikow...@gmail.com>:
> >>       at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSu pportingClassLoader.java:65)

Axel Hernández Ferrera

unread,
Feb 6, 2012, 8:56:35 AM2/6/12
to play-fr...@googlegroups.com
Same problem here, it's really annoying

Michael Pollmeier

unread,
Feb 13, 2012, 8:48:15 PM2/13/12
to play-fr...@googlegroups.com
Very annoying indeed. I've tried to get my head around this, maybe it helps in finding a workaround or bring the discussion more alive:

Play's BaseTest has the JUnit Annotation @RunWith(PlayJUnitRunner.class) which brings up the Play context.
In order to mock statics with PowerMock the test must be annotated with @RunWith(PowerMockRunner.class).
This overrides the @RunWith defined in play's BaseTest, so we cannot have both. That means that we cannot mock any of play's static methods (e.g. I'm trying to mock WS.url(String).get()) as they need the play context (at least, WS does!).

Not sure if there is an easy workaround for this. The cleanest solution is probably to replace all the statics in play with some proper dependency injection, but that's not gonna happen tomorrow I guess ;)
Another way would be to initialise play somehow. I copied a few lines from the PlayJUnitRunner but with no success:
    Play.init(new File("."), "test");
    Play.javaPath.add(Play.getVirtualFile("test"));
    Play.start();
>> play.exceptions.TemplateExecutionException: javassist.CannotCompileException: by java.io.IOException: too much data
>> Caused by: java.lang.RuntimeException: javassist.CannotCompileException: by java.io.IOException: too much data
    at org.powermock.core.classloader.MockClassLoader.loadUnmockedClass(MockClassLoader.java:187)

Looks like the Powermock and Play classloaders don't wanna play together ;(

Jeremiah Johnson

unread,
Feb 14, 2012, 12:39:06 AM2/14/12
to play-fr...@googlegroups.com
I'm hitting this in Play 1.2.4 as well.

- jeremiah

Jeremiah Johnson

unread,
Feb 14, 2012, 9:16:54 AM2/14/12
to play-fr...@googlegroups.com
Interesting, thank you.  I hadn't looked at BaseTest to see that runner.

I'm mocking out a static method in my controller and it works in Eclipse when running JUnit tests but it fails in 'play auto-test'.  As an exercise, I did also mock WS.url and I'm able to mock that in Eclipse runs.  I can't find any good documentation about Play's classloaders.  Do you know of any docs that you could point me at?

- jeremiah


--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/HweNcYh1HxMJ.

To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

 
This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed.
If you have received this email in error please notify the sender.
Please note that any views or opinions presented in this email are solely those of the author and do not necessarily represent those of the company.
Finally, the recipient should check this email and any attachments for the presence of viruses.
The company accepts no liability for any damage caused by any virus transmitted by this email.

Michael Pollmeier

unread,
Feb 14, 2012, 5:42:50 PM2/14/12
to play-fr...@googlegroups.com
So I guess you've annotated the Test with @RunWith(PowerMockRunner.class) which means you're not running in the play context. Therefore it will run fine in Eclipse but probably throw a ClassNotFoundException for the class under test if you run it in play auto-test, right?

I'm looking forward to see the response of the play team on this.

Dmitriy Arkhipov

unread,
Oct 31, 2012, 5:34:54 AM10/31/12
to play-fr...@googlegroups.com
Has anyone found way "mockito + powermock" to mock static methods in Play Framework 1.x tests?
Reply all
Reply to author
Forward
0 new messages