Skip to content

Commit 6eb1299

Browse files
Fix @mock annotation support (#270)
* Add mockito-junit-jupiter package to support @ExtendWith(MockitoExtension.class) * Add tests for mocks with annotations * Fix bug in @mock * Fix issue with generics
1 parent 9e1eed9 commit 6eb1299

File tree

6 files changed

+297
-2
lines changed

6 files changed

+297
-2
lines changed

andy/pom.xml

+7
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@
7878
<version>${mockito.version}</version>
7979
</dependency>
8080

81+
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-junit-jupiter -->
82+
<dependency>
83+
<groupId>org.mockito</groupId>
84+
<artifactId>mockito-junit-jupiter</artifactId>
85+
<version>${mockito.version}</version>
86+
</dependency>
87+
8188
<!-- org.objenesis.objenesis is needed for Mockito to work correctly with Pitest -->
8289
<!-- https://mvnrepository.com/artifact/org.objenesis/objenesis -->
8390
<dependency>

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ public boolean visit(FieldDeclaration fd) {
5151

5252
// If the field is annotated with @Mock, check if it's the class we are interested in
5353
if (hasMockAnnotation) {
54-
String className = fd.getType().toString();
55-
classWasMocked = className.contains(classToBeMocked);
54+
String className = fd.getType().resolveBinding().getBinaryName();
55+
String simpleName = className.substring(className.lastIndexOf('.') + 1);
56+
classWasMocked = simpleName.equals(classToBeMocked);
5657
}
5758
}
5859
return super.visit(fd);

andy/src/test/java/integration/CodeChecksTest.java

+11
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ void allChecksPass() {
2020
.has(codeCheck("getTripById should be set up", true, 1));
2121
}
2222

23+
@Test
24+
void mockChecksWithAnnotationPass() {
25+
Result result = run( "SoftWhereLibrary", "SoftWhereTestsWithAnnotation", "SoftWhereConfigWithCodeChecksConfiguration");
26+
27+
assertThat(result)
28+
.has(checksScore(3,3))
29+
.has(codeCheck("Trip Repository should be mocked", true, 1))
30+
.has(codeCheck("Trip should not be mocked", true, 1))
31+
.has(codeCheck("getTripById should be set up", true, 1));
32+
}
33+
2334
@Test
2435
void someChecksFail() {
2536
Result result = run( "SoftWhereLibrary", "SoftWhereTests", "SoftWhereConfigWithCodeChecks2Configuration");

andy/src/test/java/integration/JUnitTestsTest.java

+18
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,24 @@ void mocksWork() {
9191
assertThat(result.getTests().hasTestsFailingOrFailures()).isFalse();
9292
}
9393

94+
@Test
95+
void mocksWithAnnotationWork() {
96+
Result result = run(Action.TESTS, "SoftWhereLibrary", "SoftWhereTestsWithAnnotation");
97+
98+
assertThat(result.getTests().getTestsSucceeded()).isEqualTo(3);
99+
assertThat(result.getTests().getTestsRan()).isEqualTo(3);
100+
assertThat(result.getTests().hasTestsFailingOrFailures()).isFalse();
101+
}
102+
103+
@Test
104+
void mocksWithAnnotationExtendWithWork() {
105+
Result result = run(Action.TESTS, "SoftWhereLibrary", "SoftWhereTestsWithAnnotationExtendWith");
106+
107+
assertThat(result.getTests().getTestsSucceeded()).isEqualTo(3);
108+
assertThat(result.getTests().getTestsRan()).isEqualTo(3);
109+
assertThat(result.getTests().hasTestsFailingOrFailures()).isFalse();
110+
}
111+
94112
}
95113

96114

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package delft;
2+
3+
import static org.assertj.core.api.Assertions.*;
4+
import static org.junit.jupiter.api.Assertions.*;
5+
import static org.mockito.ArgumentMatchers.*;
6+
import static org.mockito.Mockito.*;
7+
8+
import java.time.LocalDate;
9+
import java.util.*;
10+
import java.util.stream.*;
11+
import org.junit.jupiter.api.*;
12+
import org.mockito.*;
13+
14+
15+
class SoftWhereTests {
16+
17+
//Methods have setup inside them, since I cannot use @BeforeEach and am
18+
//not sure if if I write a separate method to setup this will work, so yeah
19+
//there is a ton of code duplication to make sure grading will
20+
//work as expected. I also need to not have any dependency between tests,
21+
//so I cannot depend on one test making the setup. I will also not risk
22+
//putting setup into lines 26-32 without a method because I have no clue
23+
//what your grading system does and if it would work.
24+
//I have just chosen the safest way.
25+
26+
@Mock
27+
ReservationRepository reservationRepository;
28+
29+
@Mock
30+
TripRepository tripRepository;
31+
32+
SoftWhere softwhere;
33+
Person person1, person2, person3, person4, person5, person6;
34+
35+
Trip trip1, trip2, trip3;
36+
Info info1, info2, info3;
37+
38+
Reservation res1, res2, res3, res4;
39+
40+
//test when it is possible to register for the given trip
41+
@Test
42+
void testPossibleToRegister() {
43+
MockitoAnnotations.openMocks(this);
44+
45+
46+
softwhere = new SoftWhere(tripRepository, reservationRepository);
47+
48+
person1 = new Person("fname1", "mname1", "lname1", LocalDate.of(1998, 2, 5));
49+
person4 = new Person("fname4", "mname4", "lname4", LocalDate.of(1995, 11, 17));
50+
person5 = new Person("fname5", "mname5", "lname5", LocalDate.of(1978, 1, 1));
51+
info2 = new Info(LocalDate.of(2021, 7, 1), LocalDate.of(2021, 7, 15), Destination.MALTA);
52+
trip2 = new Trip("Trip to Malta", info2, 2050, 3);
53+
res4 = new Reservation(trip2, new ArrayList<>(List.of(person4, person5)));
54+
55+
when(tripRepository.getTripById(2L)).thenReturn(trip2);
56+
when(reservationRepository.getAllReservationsByTrip(trip2)).thenReturn(new ArrayList<>(List.of(res4)));
57+
58+
List<Person> people = new ArrayList<>(List.of(person1));
59+
60+
assertTrue(softwhere.makeReservation(2L, people));
61+
62+
verify(tripRepository, times(1)).getTripById(2L);
63+
verify(reservationRepository, times(1)).getAllReservationsByTrip(trip2);
64+
verify(reservationRepository, times(1)).save(new Reservation(trip2, people));
65+
66+
verifyNoMoreInteractions(tripRepository);
67+
verifyNoMoreInteractions(reservationRepository);
68+
}
69+
70+
//test when it is impossible to make a reservation
71+
//as there are not enough places left
72+
@Test
73+
void testNotEnoughPlacesLeft() {
74+
reservationRepository = mock(ReservationRepository.class);
75+
tripRepository = mock(TripRepository.class);
76+
77+
softwhere = new SoftWhere(tripRepository, reservationRepository);
78+
79+
person3 = new Person("fname3", "mname3", "lname3", LocalDate.of(1998, 11, 17));
80+
person4 = new Person("fname4", "mname4", "lname4", LocalDate.of(1995, 11, 17));
81+
82+
person1 = new Person("fname1", "mname1", "lname1", LocalDate.of(1998, 2, 5));
83+
person2 = new Person("fname2", "mname2", "lname2", LocalDate.of(2001, 4, 3));
84+
person5 = new Person("fname5", "mname5", "lname5", LocalDate.of(1978, 1, 1));
85+
person6 = new Person("fname6", "mname6", "lname6", LocalDate.of(2004, 10, 4));
86+
info1 = new Info(LocalDate.of(2021, 6, 24), LocalDate.of(2021, 6, 30), Destination.GREECE);
87+
trip1 = new Trip("Trip to Greece", info1, 1999, 5);
88+
res1 = new Reservation(trip1, new ArrayList<>(List.of(person1, person2)));
89+
res2 = new Reservation(trip1, new ArrayList<>(List.of(person5, person6)));
90+
91+
when(tripRepository.getTripById(1L)).thenReturn(trip1);
92+
when(reservationRepository.getAllReservationsByTrip(trip1)).thenReturn(new ArrayList<>(List.of(res1, res2)));
93+
94+
List<Person> people = new ArrayList<>(List.of(person3, person4));
95+
96+
assertFalse(softwhere.makeReservation(1L, people));
97+
98+
verify(tripRepository, times(1)).getTripById(1L);
99+
verify(reservationRepository, times(1)).getAllReservationsByTrip(trip1);
100+
101+
verifyNoMoreInteractions(tripRepository);
102+
verifyNoMoreInteractions(reservationRepository);
103+
}
104+
105+
//test when the wanted trip does not exist
106+
@Test
107+
void testNonexistentTrip() {
108+
reservationRepository = mock(ReservationRepository.class);
109+
tripRepository = mock(TripRepository.class);
110+
111+
softwhere = new SoftWhere(tripRepository, reservationRepository);
112+
113+
person3 = new Person("fname3", "mname3", "lname3", LocalDate.of(1998, 11, 17));
114+
115+
when(tripRepository.getTripById(4L)).thenThrow(new ElementNotFoundException());
116+
117+
List<Person> people = new ArrayList<>(List.of(person3));
118+
119+
assertFalse(softwhere.makeReservation(4L, people));
120+
121+
verify(tripRepository, times(1)).getTripById(4L);
122+
123+
verifyNoMoreInteractions(tripRepository);
124+
verifyNoMoreInteractions(reservationRepository);
125+
}
126+
}
127+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package delft;
2+
3+
import static org.assertj.core.api.Assertions.*;
4+
import static org.junit.jupiter.api.Assertions.*;
5+
import static org.mockito.ArgumentMatchers.*;
6+
import static org.mockito.Mockito.*;
7+
8+
import java.time.LocalDate;
9+
import java.util.*;
10+
import java.util.stream.*;
11+
import org.junit.jupiter.api.*;
12+
import org.mockito.*;
13+
14+
import org.junit.jupiter.api.extension.ExtendWith;
15+
import org.mockito.junit.jupiter.MockitoExtension;
16+
17+
18+
@ExtendWith(MockitoExtension.class)
19+
class SoftWhereTests {
20+
21+
//Methods have setup inside them, since I cannot use @BeforeEach and am
22+
//not sure if if I write a separate method to setup this will work, so yeah
23+
//there is a ton of code duplication to make sure grading will
24+
//work as expected. I also need to not have any dependency between tests,
25+
//so I cannot depend on one test making the setup. I will also not risk
26+
//putting setup into lines 26-32 without a method because I have no clue
27+
//what your grading system does and if it would work.
28+
//I have just chosen the safest way.
29+
30+
@Mock
31+
ReservationRepository reservationRepository;
32+
33+
@Mock
34+
TripRepository tripRepository;
35+
36+
SoftWhere softwhere;
37+
Person person1, person2, person3, person4, person5, person6;
38+
39+
Trip trip1, trip2, trip3;
40+
Info info1, info2, info3;
41+
42+
Reservation res1, res2, res3, res4;
43+
44+
//test when it is possible to register for the given trip
45+
@Test
46+
void testPossibleToRegister() {
47+
MockitoAnnotations.openMocks(this);
48+
49+
50+
softwhere = new SoftWhere(tripRepository, reservationRepository);
51+
52+
person1 = new Person("fname1", "mname1", "lname1", LocalDate.of(1998, 2, 5));
53+
person4 = new Person("fname4", "mname4", "lname4", LocalDate.of(1995, 11, 17));
54+
person5 = new Person("fname5", "mname5", "lname5", LocalDate.of(1978, 1, 1));
55+
info2 = new Info(LocalDate.of(2021, 7, 1), LocalDate.of(2021, 7, 15), Destination.MALTA);
56+
trip2 = new Trip("Trip to Malta", info2, 2050, 3);
57+
res4 = new Reservation(trip2, new ArrayList<>(List.of(person4, person5)));
58+
59+
when(tripRepository.getTripById(2L)).thenReturn(trip2);
60+
when(reservationRepository.getAllReservationsByTrip(trip2)).thenReturn(new ArrayList<>(List.of(res4)));
61+
62+
List<Person> people = new ArrayList<>(List.of(person1));
63+
64+
assertTrue(softwhere.makeReservation(2L, people));
65+
66+
verify(tripRepository, times(1)).getTripById(2L);
67+
verify(reservationRepository, times(1)).getAllReservationsByTrip(trip2);
68+
verify(reservationRepository, times(1)).save(new Reservation(trip2, people));
69+
70+
verifyNoMoreInteractions(tripRepository);
71+
verifyNoMoreInteractions(reservationRepository);
72+
}
73+
74+
//test when it is impossible to make a reservation
75+
//as there are not enough places left
76+
@Test
77+
void testNotEnoughPlacesLeft() {
78+
reservationRepository = mock(ReservationRepository.class);
79+
tripRepository = mock(TripRepository.class);
80+
81+
softwhere = new SoftWhere(tripRepository, reservationRepository);
82+
83+
person3 = new Person("fname3", "mname3", "lname3", LocalDate.of(1998, 11, 17));
84+
person4 = new Person("fname4", "mname4", "lname4", LocalDate.of(1995, 11, 17));
85+
86+
person1 = new Person("fname1", "mname1", "lname1", LocalDate.of(1998, 2, 5));
87+
person2 = new Person("fname2", "mname2", "lname2", LocalDate.of(2001, 4, 3));
88+
person5 = new Person("fname5", "mname5", "lname5", LocalDate.of(1978, 1, 1));
89+
person6 = new Person("fname6", "mname6", "lname6", LocalDate.of(2004, 10, 4));
90+
info1 = new Info(LocalDate.of(2021, 6, 24), LocalDate.of(2021, 6, 30), Destination.GREECE);
91+
trip1 = new Trip("Trip to Greece", info1, 1999, 5);
92+
res1 = new Reservation(trip1, new ArrayList<>(List.of(person1, person2)));
93+
res2 = new Reservation(trip1, new ArrayList<>(List.of(person5, person6)));
94+
95+
when(tripRepository.getTripById(1L)).thenReturn(trip1);
96+
when(reservationRepository.getAllReservationsByTrip(trip1)).thenReturn(new ArrayList<>(List.of(res1, res2)));
97+
98+
List<Person> people = new ArrayList<>(List.of(person3, person4));
99+
100+
assertFalse(softwhere.makeReservation(1L, people));
101+
102+
verify(tripRepository, times(1)).getTripById(1L);
103+
verify(reservationRepository, times(1)).getAllReservationsByTrip(trip1);
104+
105+
verifyNoMoreInteractions(tripRepository);
106+
verifyNoMoreInteractions(reservationRepository);
107+
}
108+
109+
//test when the wanted trip does not exist
110+
@Test
111+
void testNonexistentTrip() {
112+
reservationRepository = mock(ReservationRepository.class);
113+
tripRepository = mock(TripRepository.class);
114+
115+
softwhere = new SoftWhere(tripRepository, reservationRepository);
116+
117+
person3 = new Person("fname3", "mname3", "lname3", LocalDate.of(1998, 11, 17));
118+
119+
when(tripRepository.getTripById(4L)).thenThrow(new ElementNotFoundException());
120+
121+
List<Person> people = new ArrayList<>(List.of(person3));
122+
123+
assertFalse(softwhere.makeReservation(4L, people));
124+
125+
verify(tripRepository, times(1)).getTripById(4L);
126+
127+
verifyNoMoreInteractions(tripRepository);
128+
verifyNoMoreInteractions(reservationRepository);
129+
}
130+
}
131+

0 commit comments

Comments
 (0)