Skip to content

Commit 7788b3a

Browse files
Fix Mockito do-syntax (#274)
* Fix do-syntax in tests * Fix do-syntax handling in MockitoWhen
1 parent 1c0e05e commit 7788b3a

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

andy/src/main/java/nl/tudelft/cse1110/andy/codechecker/checks/MockitoWhen.java

+5-14
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,12 @@ public boolean visit(MethodInvocation mi) {
4747
* If the method call just after when() matches the one we are expecting, bingo!
4848
*/
4949
if(inWhenMode) {
50-
if(methodToSetUp.equals(methodName) || methodToSetUp.equals(previousMethodName)) {
50+
if(methodToSetUp.equals(methodName) ||
51+
methodToSetUp.equals(previousMethodName) && allowedDoInvocations.contains(methodName)) {
5152
numberOfCallsToWhen++;
52-
inWhenMode = false;
53-
previousMethodName = "";
54-
} else if(notInDoWhenOrNoMatch(methodName)){
55-
inWhenMode = false;
56-
previousMethodName = "";
57-
} else {
58-
previousMethodName = methodName;
5953
}
54+
inWhenMode = false;
55+
previousMethodName = "";
6056
} else {
6157
/**
6258
* We wait for a call to when().
@@ -70,12 +66,7 @@ public boolean visit(MethodInvocation mi) {
7066
}
7167
return super.visit(mi);
7268
}
73-
private boolean notInDoWhenOrNoMatch(String methodName){
74-
return !methodName.equals("when")
75-
&&
76-
(allowedDoInvocations.contains(previousMethodName)
77-
|| !allowedDoInvocations.contains(methodName));
78-
}
69+
7970
@Override
8071
public boolean result() {
8172
return comparison.compare(numberOfCallsToWhen, expectedNumberOfOccurrences);

andy/src/test/resources/codechecker/fixtures/MockitoWhenCalls.java

+18-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ void t2() {
3636
void t3(){
3737
List<String> mockedList = mock(List.class);
3838
Mockito.doThrow(IllegalArgumentException.class).when(mockedList).toString();
39-
doNothing().when(mockedList.size());
39+
doNothing().when(mockedList).size();
4040
doReturn("2").when(mockedList).get(any(Integer.class));
4141

4242
verify(mockedList, times(2)).toString();
@@ -46,9 +46,9 @@ void t3(){
4646
@Test
4747
void t4(){
4848
List<String> mockedList = mock(List.class);
49-
Mockito.doNothing().when(mockedList.size());
50-
doReturn("2").when(mockedList.get(any(Integer.class)));
51-
doReturn("3").when(mockedList.remove(any(Integer.class)));
49+
Mockito.doNothing().when(mockedList).size();
50+
doReturn("2").when(mockedList).get(any(Integer.class));
51+
doReturn("3").when(mockedList).remove(any(Integer.class));
5252

5353
verify(mockedList, times(1)).remove(1);
5454
verify(mockedList, times(1)).remove(2);
@@ -57,4 +57,18 @@ void t4(){
5757
verify(mockedList, times(1)).get(5);
5858
}
5959

60+
@Test
61+
void t5(){
62+
List<String> mockedList = mock(List.class);
63+
Mockito.doReturn(3).when(mockedList).size();
64+
mockedList.get(3);
65+
}
66+
67+
@Test
68+
void t6(){
69+
List<String> mockedList = mock(List.class);
70+
Mockito.doReturn(3).when(mockedList.get(3));
71+
mockedList.get(3);
72+
}
73+
6074
}

0 commit comments

Comments
 (0)