我正在使用spring 3 mvc/security框架。在不使用@RunWith的情況下在Spring單元測試中連接依賴關係
我已經創建了一個Controller類,它具有對從中加載數據的存儲庫的引用。該類用@Controller
進行註釋,存儲庫類用@Repository
進行註釋,並且存儲庫的實例爲@Autowired
。
但是當我嘗試單元測試時,autowired實例會拋出一個空指針異常。
現在,我明白了,因爲它是自動裝配的,它需要在春天的環境中被挑選出來。但我覺得如果我使用@RunsWith()
那麼它就成爲一個綜合測試。我真的很想分開集成測試(使用@RunsWith
)和單元測試。關於如何解決這個空指針異常的任何想法?只想創造我的控制器類的getter/setter方法是好的?:
庫類:
@Repository
public class Repository{
....
}
控制器類:
@Controller
public class Controller{
@Autowired
private Repository repo;
....
public String showView(){
repo.doSomething();
}
測試類:
public ControllerTest {
@Test
public shouldDoTestOfShowView(){
}
}
那麼在控制器中創建getter/setter是最佳實踐嗎? – 2012-02-09 15:02:43