How Can I Avoid Guice Tapered Tile Code

How do I avoid a conical bowl plate?

With Spring, I usually use @AutoWired, and this is it, no createInjector, no injectMemebers (this). Is there any way to avoid using while using giuce? Are there any global settings that I can do, will I automatically load all things into application 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);

    }
}
+4
source share
2 answers

I think you are looking for something like 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());
   }
}
+2
source

, . " ", Guice 4 (. wiki page)

, , .

0

Source: https://habr.com/ru/post/1611971/


All Articles