[英]Mocking String.valueof()
我正在測試彈簧控制器,並嘗試執行以下操作
控制者
Authentication authentication = SecurityContextHolder.getContext()
.getAuthentication();
String role = String.valueOf(authentication.getAuthorities());
if(role.contains("user")) {
...
}
測試
@Test
public void testLoginUser() throws Exception {
User user = new User();
user.setLogin("user");
user.setRoleid(1L);
SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(user,user));
when(String.valueOf(Collection.class)).thenReturn("user");
但是我明白了
org.mockito.exceptions.misusing.MissingMethodInvocationException
我需要if
塊為true
並執行。
有什么辦法可以做到嗎?
String.valueOf
是一個靜態方法,Mockito並未對其進行模擬。 您的表達式將簡單地解析為值為"class java.util.Collection"
的String實例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.