假設我有一個名為UserController的控制器類,它有兩個方法:getUserCount()和getLatestUser(),getUserCount 呼叫 getLatestUser。
@Controller
class UserController{
public long getUserCount(){
#code
getLatestUser();
#code
}
public User getLatestUser(){}
}
我應該使用 Junit 和 Mockito 測驗這些方法中的每一個,因此我有這樣的東西:
class UserControllerTest{
@Autowired
UserController userController;
@Test
public void testing_get_user_count(){
User user = new User();
when(userController.getLastestUser()).thenReturn(user);
}
}
我的問題是我不能模擬 UserController,因為我已經自動連接了它,所以我不能在 getLatestUser 上使用 when().thenReturn()。
有沒有辦法讓我嘲笑它?
uj5u.com熱心網友回復:
您可以使用@SpyBean而不是@Autowired. 它在 bean 上應用 Mockito spy。
class UserControllerTest {
@SpyBean
UserController userController;
@Test
public void testing_get_user_count(){
User user = new User();
when(userController.getLastestUser()).thenReturn(user);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/406796.html
標籤:
下一篇:單元測驗回呼流
