How do I avoid guice boiler plate?
With Spring, I usually use @AutoWired and thats it, no createInjector, no injectMemebers(this). Is there a way to avoid those while using giuce? Is there some global settings that I can do so I'll automatically have all things injected in app and test classes?
public class AccountDaoTest {
@Before
public void setup() {
Injector injector;
injector = Guice.createInjector();// I don't want this boiler-plate
injector.injectMembers(this);
}
@Inject
AccountDAO accountDao ;
@Test
public void persist(){
Account ac = new Account();
ac.setName("AccountName");
ac.setAccountType(AccountType.Hospital);
accountDao.createAccount(ac);
}
}
I guess you are looking for something like the SpringJunitRunner for guice: https://github.com/caarlos0/guice-junit-test-runner
@RunWith(GuiceTestRunner.class)
@GuiceModules(MyModule.class)
public class MyTest {
@Inject
private Something something;
@Test
public void testItWorks() throws Exception {
Assert.assertThat(something.doSomething(), CoreMatchers.notNullValue());
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With