开发者

How I get Guice and TestNg playing along, a-la-AtUnit style

开发者 https://www.devze.com 2023-01-27 06:25 出处:网络
I\'m 开发者_JAVA技巧using TestNG for unit & integration tests, and Guice for DI. I would like to treat my test classes like other classes - instead of directly working with the injector, I would r

I'm 开发者_JAVA技巧using TestNG for unit & integration tests, and Guice for DI. I would like to treat my test classes like other classes - instead of directly working with the injector, I would rather define fields marked with @Inject, and get these injected before each test.

Is there a framework that does this? I believe that AtUnit does something similar, but I'm not sure how it integrates with TestNG, nor how mature it is.


Cedric has now added a @Guice annotation directly into TestNG to facilitate this use case.

  • Docs: http://testng.org/doc/documentation-main.html#guice-dependency-injection (up to date)
  • Background post: http://beust.com/weblog/2010/12/10/testng-and-guice-a-marriage-made-in-heaven/ (slightly changed since post!)


TestNG uses Guice internally but it also makes it very easy to have your test classes injected before TestNG sees them (using Guice or whatever other framework you prefer).

All you need to do is implement IObjectFactory and then declare your class name when you launch TestNG with -objectfacfory.

Then TestNG will delegate all object creations to your factory, which allows you to create your instances and inject them before returning them to TestNG.


For an example of how to use this and have it interact with TestNG.xml groups look at my blog entry http://biggerwrench.blogspot.com/2014/02/testng-using-guice-for-dependency.html

0

精彩评论

暂无评论...
验证码 换一张
取 消