다음은 WorkWithArrayList
의 예제 클래스를 볼 수 있습니다. 이 클래스에는 removeFirstThree
과 insertData
의 두 가지 메소드가 있습니다. removeFirstThree
메서드는 List
을 인수로 가져 와서 수정하고 데이터베이스에 삽입합니다. for
루프에서는 List
의 첫 번째 요소 인 Integer
을 제거한 것처럼이 수정을 보여주고 모든 반복에 데이터를 삽입합니다.Mockito에 의해 예상 인수 (java.util.List를 인수로)가 호출되었는지 확인하는 방법
깨닫고 싶은 건 insertData
메서드의 인수를 확인하는 것입니다. 하지만 verify
첫 번째 방법은 verify(workWithArrayList).insertData(expected);
선언으로만 확인합니다.
package test;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
public class MockitoVerifyWithArrayListTest {
private WorkWithArrayList workWithArrayList;
private List<Integer> actual;
private List<Integer> expected;
@Before
public void setUp() throws Exception {
workWithArrayList = spy(new WorkWithArrayList());
actual = new ArrayList<>();
actual.add(1);
actual.add(2);
actual.add(3);
actual.add(4);
actual.add(5);
actual.add(6);
expected = new ArrayList<>();
expected.add(1);
expected.add(2);
expected.add(3);
expected.add(4);
expected.add(5);
expected.add(6);
}
@Test
public void test() throws Exception {
workWithArrayList.removeFirstThree(actual);
expected.remove(0);
verify(workWithArrayList).insertData(expected);
expected.remove(0);
verify(workWithArrayList).insertData(expected);
expected.remove(0);
verify(workWithArrayList).insertData(expected);
}
public class WorkWithArrayList {
public void removeFirstThree(List<Integer> integers) {
for (int i = 0; i < 3; i++) {
integers.remove(0);
insertData(integers);
}
}
public void insertData(List<Integer> integers) {
}
}
}
나는 다음과 같은 오류에 직면하고이 테스트를 실행하면 :
는Argument(s) are different! Wanted:
workWithArrayList.insertData(
[2, 3, 4, 5, 6]
);
-> at test.MockitoVerifyWithArrayListTest.test(MockitoVerifyWithArrayListTest.java:46)
Actual invocation has different arguments:
workWithArrayList.insertData(
[4, 5, 6]
);
-> at test.MockitoVerifyWithArrayListTest.test(MockitoVerifyWithArrayListTest.java:43)
편집 : 우리가 Mockito.verify
방법의 javadoc에 보면, 우리는이를 볼 수 있습니다 통과
인수됩니다 비교 방법은 equals()
입니다.
하지만 변수 actual
과 expected
은 동일하며 둘 다 첫 번째 요소를 제거하더라도 동일하게 유지됩니다. 왜이 테스트가 실패하는지 나는 흥미 롭다.