Mockito 返回传递的 ArrayList 的相同参数
Mockito returning same argument of ArrayList which is passed
我有一个方法用作:
List<Integer> permittedUserIds= this.getAuthorizationManager()
.getPermittedUserIDs(Constants.Permissions.Target.COMMON_DATA, Constants.Permissions.Action.READ,
userIdList);
模拟方法如下:
选项 1:
Mockito.when(spied.getPermittedUserIDs(Mockito.anyString(),Mockito.anyString(),Mockito.anyList())).thenAnswer(i -> i.getArguments()[2]);
这甚至不起作用,因为我认为只有 doReturn 需要起作用,thenAnswer 在这里不起作用。
选项 2:
Mockito.doReturn(AdditionalAnswers.returnsLastArg())
.when(spied)
.getPermittedUserIDs(Mockito.anyString(), Mockito.anyString(),
Mockito.anyList());
获取运行时异常为:
ERROR! java.lang.RuntimeException:
org.mockito.exceptions.misusing.WrongTypeOfReturnValue:
ReturnsArgumentAt cannot be returned by getPermittedUserIDs()
getPermittedUserIDs() should return List
If you're unsure why you're getting above error read on. Due to the
nature of the syntax above problem might occur because:
- This exception might occur in wrongly written multi-threaded tests. Please refer to Mockito FAQ on limitations of concurrency
testing.
- A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies -
- with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method.
请建议如何模拟它。这个想法是 return 作为最后一个参数传入的相同数组列表。
使用 doAnswer
而不是 doReturn
。
示例如下:
public class AppTest {
@Test
void test() {
Service service = Mockito.mock(Service.class);
List<Integer> userIdList = Arrays.asList(3,4);
Mockito.doAnswer(AdditionalAnswers.returnsLastArg())
.when(service)
.getPermittedUserIDs(Mockito.anyString(), Mockito.anyString(),
Mockito.anyList());
List<Integer> permittedUserIds= service
.getPermittedUserIDs("1", "2",
userIdList);
Assertions.assertSame(userIdList,permittedUserIds);
}
public interface Service {
List<Integer> getPermittedUserIDs(String a, String b, List<Integer> userIdList);
}
}
我有一个方法用作:
List<Integer> permittedUserIds= this.getAuthorizationManager()
.getPermittedUserIDs(Constants.Permissions.Target.COMMON_DATA, Constants.Permissions.Action.READ,
userIdList);
模拟方法如下:
选项 1:
Mockito.when(spied.getPermittedUserIDs(Mockito.anyString(),Mockito.anyString(),Mockito.anyList())).thenAnswer(i -> i.getArguments()[2]);
这甚至不起作用,因为我认为只有 doReturn 需要起作用,thenAnswer 在这里不起作用。
选项 2:
Mockito.doReturn(AdditionalAnswers.returnsLastArg())
.when(spied)
.getPermittedUserIDs(Mockito.anyString(), Mockito.anyString(),
Mockito.anyList());
获取运行时异常为:
ERROR! java.lang.RuntimeException: org.mockito.exceptions.misusing.WrongTypeOfReturnValue: ReturnsArgumentAt cannot be returned by getPermittedUserIDs() getPermittedUserIDs() should return List
If you're unsure why you're getting above error read on. Due to the nature of the syntax above problem might occur because:
- This exception might occur in wrongly written multi-threaded tests. Please refer to Mockito FAQ on limitations of concurrency testing.
- A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies -
- with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method.
请建议如何模拟它。这个想法是 return 作为最后一个参数传入的相同数组列表。
使用 doAnswer
而不是 doReturn
。
示例如下:
public class AppTest {
@Test
void test() {
Service service = Mockito.mock(Service.class);
List<Integer> userIdList = Arrays.asList(3,4);
Mockito.doAnswer(AdditionalAnswers.returnsLastArg())
.when(service)
.getPermittedUserIDs(Mockito.anyString(), Mockito.anyString(),
Mockito.anyList());
List<Integer> permittedUserIds= service
.getPermittedUserIDs("1", "2",
userIdList);
Assertions.assertSame(userIdList,permittedUserIds);
}
public interface Service {
List<Integer> getPermittedUserIDs(String a, String b, List<Integer> userIdList);
}
}