3
我有使用澤西2依賴注入單元測試
ResourceConfig config = new ResourceConfig()
.packages("controller", "exception")
.register(new MyDIBinder());
哪裏MyDIBinder是這樣
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class AccountController implements CRUDController<Long, Account> {
private AccountDao accountDao;
private AccountService accountService;
@Inject
public AccountController(AccountDao accountDao, AccountService accountService) {
this.accountDao = accountDao;
this.accountService = accountService;
}
...
我注入AccountDao和的AccountService控制器包含的所有綁定(例如
AccountDaoImpl accountDaoImpl = new AccountDaoImpl();
bind(accountDaoImpl).to(AccountDao.class);
)
現在我想爲這個控制器編寫一個單元測試,是否有可能將整個AccountController實例及其所有傳遞依賴注入到測試中?
喜歡的東西
@Inject
AccountController accountController;
謝謝你的回答!什麼是TestBinder? – andreybavt
這是我用來測試我的例子的活頁夾。它只包含一個簡單的服務綁定。對你而言,這將是MyDIBinding –
@peeskillet請使用TestBinder示例完成此響應。 – 4F2E4A2E