Java Mockito:Mock一种操作输入数组参数的方法
如何模拟一个以int数组作为输入并操作数组元素的方法?我相信我们可以用doAnswer实现这一点,但我无法在这里表示输入类型Java Mockito:Mock一种操作输入数组参数的方法,java,android,junit,mockito,Java,Android,Junit,Mockito,如何模拟一个以int数组作为输入并操作数组元素的方法?我相信我们可以用doAnswer实现这一点,但我无法在这里表示输入类型 class View { public void getLocationOnScreen(int[] location) { //this method assigns elements of the array. location[0] = 5; location[1] = 6; } } 如果您进行模拟,测试将不会通过该方法。因此,您并
class View {
public void getLocationOnScreen(int[] location) {
//this method assigns elements of the array.
location[0] = 5;
location[1] = 6;
}
}
如果您进行模拟,测试将不会通过该方法。因此,您并不真正关心该方法中的逻辑,而是返回一种您将等待的对象 您的位置是一个void方法,因此它不会返回任何内容。所以你只需要做这样的事情:
Mockito.when getLocationScreenMockito.anyint[]类 如果进行模拟,测试将无法通过该方法。因此,您并不真正关心该方法中的逻辑,而是返回一种您将等待的对象 您的位置是一个void方法,因此它不会返回任何内容。所以你只需要做这样的事情: Mockito.when getLocationScreenMockito.anyint[]类 试试这个
Mockito.doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
int[] location = (int[]) args[0];
return location;
}
}).when(view).getLocationOnScreen(Matchers.any(int []));
试试这个
Mockito.doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
int[] location = (int[]) args[0];
return location;
}
}).when(view).getLocationOnScreen(Matchers.any(int []));
看看这是否是你所期望的:
@Test
public void getLocationOnScreen() throws Exception {
View mockView = mock(View.class);
// Prepare the integer array to test.
int[] location = new int[2];
// Verify interactions
mockView.getLocationOnScreen(location);
verify(mockView).getLocationOnScreen(location);
// Stub voids
doAnswer(new Answer() {
@Override
public int[] answer(InvocationOnMock invocation) throws Throwable {
int[] args = (int[])(invocation.getArguments()[0]);
args[0] = 5;
args[1] = 6;
return args;
}
}).when(mockView).getLocationOnScreen(location);
// Asserts
mockView.getLocationOnScreen(location);
assertEquals(5, location[0]);
assertEquals(6, location[1]);
}
看看这是否是你所期望的:
@Test
public void getLocationOnScreen() throws Exception {
View mockView = mock(View.class);
// Prepare the integer array to test.
int[] location = new int[2];
// Verify interactions
mockView.getLocationOnScreen(location);
verify(mockView).getLocationOnScreen(location);
// Stub voids
doAnswer(new Answer() {
@Override
public int[] answer(InvocationOnMock invocation) throws Throwable {
int[] args = (int[])(invocation.getArguments()[0]);
args[0] = 5;
args[1] = 6;
return args;
}
}).when(mockView).getLocationOnScreen(location);
// Asserts
mockView.getLocationOnScreen(location);
assertEquals(5, location[0]);
assertEquals(6, location[1]);
}
您可以使用lambda表达式以更简洁的方式执行此操作
doAnswer(invocation -> {
Object[] args = invocation.getArguments();
int[] location = (int[]) args[0];
return location;
}).when(view).getLocationOnScreen(Matchers.any(int []));
您可以使用lambda表达式以更简洁的方式执行此操作
doAnswer(invocation -> {
Object[] args = invocation.getArguments();
int[] location = (int[]) args[0];
return location;
}).when(view).getLocationOnScreen(Matchers.any(int []));
我需要在调用该方法时为数组的元素设置模拟值。在调用getLocationOnScreen方法后,这些更新后的数组元素将在进一步的逻辑中使用。因此,我认为应该有某种方法在调用该方法时模拟数组的元素。我需要在调用该方法时为数组的元素设置模拟值。在调用getLocationOnScreen方法后,这些更新后的数组元素将在进一步的逻辑中使用。所以我相信在调用该方法时应该有某种方法来模拟数组的元素。这很有效。只有一点零钱。我没有说Matchers.any,而是传递了一个整数数组。这很有效。只有一点零钱。我没有说Matchers.any,而是传递了一个整数数组。与Ahmed的答案相同,但更详细。谢谢。与艾哈迈德的回答相同,但更详细。谢谢