How to prevent double-instantiating of a module?

38 views
Skip to first unread message

Kohei Nozaki

unread,
Mar 30, 2016, 8:54:31 AM3/30/16
to Jukito
Hi, thank you for inventing Jukito. I'm learning Jukito to eliminate boilarplate code in my integration test cases of service classes built upon Guice.

I found that Jukito instantiates a module twice in single test execution. my module involves some of slow instantiation process and it makes execution time of my test cases longer.

So I'm looking for a way to eliminate redundant module instantiation, hopefully I want do it only once. how can I achieve that? or is there any reason that I can't achieve that? any advices would be greatly appreciated.

I've pushed a reproducer to GitHub:


Thanks.

Christopher Viel

unread,
Apr 1, 2016, 10:51:19 AM4/1/16
to Jukito
Jukito uses some Guice utils to extract all bindings to then create the appropriate mocks and register the right bindings. Afaik, this utility executes the configure() method to capture the bindings, so here's the first execution you notice. The second one is the "real" execution -- called by the Guice.createInjector(). You should avoid making computations in configure. The alternative would be to create add a factory method in your module and annotate it with @Produces (and a scope annotation depending on your use case).

Kohei Nozaki

unread,
Apr 2, 2016, 1:11:17 AM4/2/16
to Jukito
Hi Christopher,

Thank you for the clear explanation, I've just digged Jukito's source code and reached the same conclusion as yours. extracting bindings seem necessary to make Jukito work.

I agree with your solution, it makes sense. but the most slow instantiation process in my module is JPA provider initialization caused by guice-persist. it takes time and I think it can't be moved to a factory method.

Any idea to improve my situation would be appreciated,

Thanks.

2016年4月1日金曜日 23時51分19秒 UTC+9 Christopher Viel:
Reply all
Reply to author
Forward
0 new messages