如何避免guice锅炉板?如何避免Guice锅炉板代码
随着春天,我通常使用@AutoWired和多数民众赞成在它,没有createInjector,没有injectMemebers(this)。有没有办法避免使用giuce的人呢?是否有一些我可以做的全局设置,我会自动将所有东西注入应用程序和测试类中?
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);
}
}
编写一个基类或JUnit'@ Rule'来为你做这件事吗? –
@Tavian,我宁愿避免“MyTest扩展BaseTest”。在Test类中寻找类似于_AT_WithGuice的注释行。 – Espresso
那么'@ Rule'可以避免扩展一个基类。还有Jukito和@ Jan的建议。 –