I’m trying Guice. I made a simple example which is very similar to the example in the Guice Getting Started manual.
But it didn’t work for some time and said:
Could not find a suitable constructor
in mypack.ServiceUser. Classes must
have either one (and only one)
constructor annotated with @Inject or
a zero-argument constructor that is
not private. at
mypack.ServiceUser.class(ServiceUser.java:14)
while locating mypack.ServiceUser
The problem was in imports. I attached both Google Guice and TestNG to the project. And I imported classes from org.testng.guice package. But annotation Inject was imported from the javax.inject package. After I changed that import to org.testng.guice.Inject my program began working.
But after that I played with imports a little more. And I changed all org.testng imports to com.google.inject. The program still worked. After that I went back to the @Inject annotation and changed its import to javax.inject.Inject (as it was in my very first broken version). And the program worked even after that.
I checked ones again: Guice from TestNG packages doesn’t work with javax.inject.Inject while Guice from google packages (com.google.inject) works normally with it.
Why is it so weird?
p.s. I use last versions of both Guice and TestNG (guice-3.0-rc1 and testng-5.14.5).
The short answer is: @Guice is still a work in progress and being designed on the mailing-list as we speak, so it’s still experimental.
Also, you are using undocumented packages that are reserved for TestNG’s internal use (org.testng.guice).
I pushed a new version just today, you can download it at http://testng.org/beta
Note that this new version no longer has org.testng.guice, so make sure that whenever you need Guice symbols, you import them from com.google.inject.
Please post on the testng-users mailing-list if you have any more questions/concerns.
Thanks.