How to test Guice Singleton?

Posted by 01 on Stack Overflow See other posts from Stack Overflow or by 01
Published on 2010-05-22T07:21:29Z Indexed on 2010/05/22 7:30 UTC
Read the original article Hit count: 183

Filed under:
|
|
|
|

Guice Singletons are weird for me

First I thought that

IService ser = Guice.createInjector().getInstance(IService.class);
System.out.println("ser=" + ser);
ser = Guice.createInjector().getInstance(IService.class);
System.out.println("ser=" + ser);

will work as singleton, but it returns

ser=Service2@1975b59
ser=Service2@1f934ad

its ok, it doesnt have to be easy.

Injector injector = Guice.createInjector();
IService ser = injector.getInstance(IService.class);
System.out.println("ser=" + ser);
ser = injector.getInstance(IService.class);
System.out.println("ser=" + ser);

works as singleton

ser=Service2@1975b59
ser=Service2@1975b59

So i need to have static field with Injector(Singleton for Singletons)

how do i pass to it Module for testing?

© Stack Overflow or respective owner

Related posts about guice

Related posts about singleton