Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I avoid Guice boiler-plate code

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);

    }
}
like image 690
Espresso Avatar asked Oct 30 '22 17:10

Espresso


1 Answers

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());
   }
}
like image 74
Jan Galinski Avatar answered Dec 21 '22 15:12

Jan Galinski