我測試我的春天控制器和嘗試做以下嘲諷將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
並執行。
有什麼辦法可以做到?
你看過PowerMock嗎?請參閱https://code.google.com/p/powermock/wiki/MockStatic和https://code.google.com/p/powermock/wiki/MockFinal。 –