我该如何避免使用锅炉板?
使用Spring,我通常使用@AutoWired就可以了,没有createInjector,没有injectMemebers(这个).有没有办法在使用giuce时避免这些?我可以做一些全局设置,所以我会在app和测试类中自动注入所有东西吗?
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);
}
}
最佳答案 我猜你正在寻找像SpringJunitRunner这样的东西:
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());
}
}