{"id":37,"date":"2011-11-11T16:58:10","date_gmt":"2011-11-11T15:58:10","guid":{"rendered":"http:\/\/www.flamingpenguin.co.uk\/blog\/?p=37"},"modified":"2011-11-11T17:47:19","modified_gmt":"2011-11-11T16:47:19","slug":"integrating-guice-and-junit4","status":"publish","type":"post","link":"https:\/\/www.flamingpenguin.co.uk\/blog\/2011\/11\/11\/integrating-guice-and-junit4\/","title":{"rendered":"Integrating guice and junit4"},"content":{"rendered":"<p>A JUnit4 Method rule to perform injection on your test fixture before each test case:<\/p>\n<pre lang=\"java\">\r\nimport com.google.inject.Guice;\r\nimport com.google.inject.Injector;\r\nimport com.google.inject.Module;\r\n\r\nimport org.junit.rules.MethodRule;\r\nimport org.junit.runners.model.FrameworkMethod;\r\nimport org.junit.runners.model.Statement;\r\n\r\npublic class PerTestGuiceRule implements MethodRule\r\n{\r\n   private final Module module;\r\n\r\n   public PerTestGuiceRule(final Module module)\r\n   {\r\n      this.module = module;\r\n   }\r\n\r\n   @Override\r\n   public Statement apply(\r\n            final Statement base,\r\n            @SuppressWarnings(\"unused\") final FrameworkMethod method,\r\n            final Object target)\r\n   {\r\n      return new Statement()\r\n      {\r\n         @Override\r\n         public void evaluate() throws Throwable\r\n         {\r\n            final Injector injector = Guice.createInjector(module);\r\n\r\n            injector.injectMembers(target);\r\n\r\n            base.evaluate();\r\n         }\r\n      };\r\n   }\r\n}\r\n<\/pre>\n<p>Use it by putting this code in you test fixture:<\/p>\n<pre lang=\"java\">\r\n@Rule public PerTestGuiceRule guiceRule = \r\n                   new PerTestGuiceRule(new MyModule());\r\n   \r\n@Inject MyInjectableType myInjectableType;\r\n<\/pre>\n<p>You might want to check out <a href=\"http:\/\/code.google.com\/p\/guiceberry\/\" title=\"guiceberry\">GuiceBerry<\/a>. I tried to use it, but for my case it seems very complex, or maybe impossible, to create your module per-test due to the static map of module class -> module instance used internally by guiceberry. There is a test scope included, but I don&#8217;t see a really clean way to use that if you want to test your production guice modules.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A JUnit4 Method rule to perform injection on your test fixture before each test case: import com.google.inject.Guice; import com.google.inject.Injector; import com.google.inject.Module; import org.junit.rules.MethodRule; import org.junit.runners.model.FrameworkMethod; import org.junit.runners.model.Statement; public class PerTestGuiceRule implements MethodRule { private final Module module; public PerTestGuiceRule(final Module &hellip; <a href=\"https:\/\/www.flamingpenguin.co.uk\/blog\/2011\/11\/11\/integrating-guice-and-junit4\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27,7],"tags":[],"class_list":["post-37","post","type-post","status-publish","format-standard","hentry","category-guice","category-java"],"_links":{"self":[{"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/posts\/37","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=37"}],"version-history":[{"count":0,"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/posts\/37\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=37"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=37"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=37"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}