Java - play.api.PlayException: Cannot load module[Module [TestModule] cannot be instantiated.

3,161 views
Skip to first unread message

Dexter Ang

unread,
Mar 8, 2016, 1:26:45 PM3/8/16
to play-framework
I'm encountering the error as per title while trying to follow the DI guide here: https://www.playframework.com/documentation/2.4.x/JavaDependencyInjection

This occurs when running my application normally, but doesn't happen when running my test no matter where I place my module.


play.modules.enabled += "TestModule"

public class TestModule extends AbstractModule {
    protected void configure() {
        bind(A.class).to(ADefault.class);
    }
}

I could solve this issue by using @ImplementedBy, however I have lots of classes and I thought it would be great to manage all in 1 place.
I'm also very curious why my setup only works when running the test, which will only give me the error when I purposely set the wrong thing in application.conf.

Is there any other configurations I'm missing?

Dexter Ang

unread,
Mar 8, 2016, 1:31:47 PM3/8/16
to play-framework
Oh and I also tried putting it in the root as well and commented play.modules.enabled. This will just give me "No implementation was bound" kind of error when running the application, which proves that Play isn't even looking at the module class at all. Running my tests does not give me such error. 
 

Greg Methvin

unread,
Mar 8, 2016, 2:12:00 PM3/8/16
to play-fr...@googlegroups.com
Are you using the fully qualified class name for the module? e.g. com.example.TestModule


On Tue, Mar 8, 2016 at 10:32 AM -0800, "Dexter Ang" <hito...@gmail.com> wrote:

Oh and I also tried putting it in the root as well and commented play.modules.enabled. This will just give me "No implementation was bound" kind of error when running the application, which proves that Play isn't even looking at the module class at all. Running my tests does not give me such error. 
 

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/8680bc18-2f7e-4da9-9345-efb02b506a6b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dexter Ang

unread,
Mar 8, 2016, 2:18:06 PM3/8/16
to play-framework
Yes, in the example I provided, it was in the default test folder, so it is already fully qualified. And if it isn't, running the test should give error as well.

Greg Methvin

unread,
Mar 8, 2016, 4:07:09 PM3/8/16
to play-framework
You said it was in the default test folder? If so the behavior is as expected. You should not be able to access test classes when running the application normally.

If you want to load that class when running your application normally, put it in the app folder. If you want to load it only for tests, you should create a different configuration file in test that references the module.


For more options, visit https://groups.google.com/d/optout.



--
Greg Methvin
Senior Software Engineer

Message has been deleted

Dexter Ang

unread,
Mar 8, 2016, 9:51:13 PM3/8/16
to play-framework
Thank you very much! It's working as expected now. I'm not surprised that it doesn't work in the test folder, but I'm surprised that it doesn't work in the root either.
So right now I have 2 exact same Module - 1 in app, and the other in root. It's working handily, but may I clarify what you mean by "create a different configuration file in test that references the module"? It sounds like I can avoid duplicate code. Thanks!

Greg Methvin

unread,
Mar 9, 2016, 12:32:01 AM3/9/16
to play-framework
Here's a good post on using a different config for testing: http://www.pmatiello.me/2014/12/using-a-different-configuration-file-when-running-tests-in-a-play-project.html

If you want the module available in both I would put it in the app folder and just add it in to play.modules.enabed conf/application.conf. If you want it in test only you could put the module in the test folder and add it to play.modules.enabled in your test configuration file. Or you could just add the module explicitly to your GuiceApplicationBuilder in your tests and have no custom test configuration at all.


For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages