Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit b7413d3

Browse files
author
Aleksi Salmela
committed
Mock the TmcUtils instead of core in all tests.
1 parent 5ec1065 commit b7413d3

12 files changed

Lines changed: 274 additions & 495 deletions

src/main/java/fi/helsinki/cs/tmc/cli/command/LoginCommand.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package fi.helsinki.cs.tmc.cli.command;
22

33
import fi.helsinki.cs.tmc.cli.Application;
4-
import fi.helsinki.cs.tmc.cli.CliContext;
54
import fi.helsinki.cs.tmc.cli.command.core.AbstractCommand;
65
import fi.helsinki.cs.tmc.cli.command.core.Command;
76
import fi.helsinki.cs.tmc.cli.io.Io;

src/main/java/fi/helsinki/cs/tmc/cli/command/RunTestsCommand.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
import java.util.List;
2929

30-
3130
@Command(name = "test", desc = "Run local exercise tests")
3231
public class RunTestsCommand extends AbstractCommand {
3332

src/test/java/fi/helsinki/cs/tmc/cli/command/CourseInfoCommandTest.java

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,35 @@
33
import static org.junit.Assert.assertFalse;
44
import static org.junit.Assert.assertNotNull;
55
import static org.junit.Assert.assertTrue;
6-
import static org.mockito.Matchers.any;
6+
import static org.mockito.Matchers.eq;
77
import static org.mockito.Mockito.doReturn;
88
import static org.mockito.Mockito.mock;
99
import static org.mockito.Mockito.spy;
1010
import static org.mockito.Mockito.when;
1111

1212
import fi.helsinki.cs.tmc.cli.Application;
1313
import fi.helsinki.cs.tmc.cli.io.TestIo;
14+
import fi.helsinki.cs.tmc.cli.tmcstuff.TmcUtil;
1415
import fi.helsinki.cs.tmc.cli.tmcstuff.WorkDir;
1516
import fi.helsinki.cs.tmc.core.TmcCore;
1617
import fi.helsinki.cs.tmc.core.domain.Course;
1718
import fi.helsinki.cs.tmc.core.domain.Exercise;
18-
import fi.helsinki.cs.tmc.core.domain.ProgressObserver;
1919

2020
import org.junit.Before;
2121
import org.junit.BeforeClass;
2222
import org.junit.Test;
23+
import org.junit.runner.RunWith;
24+
import org.powermock.api.mockito.PowerMockito;
25+
import org.powermock.core.classloader.annotations.PrepareForTest;
26+
import org.powermock.modules.junit4.PowerMockRunner;
2327

2428
import java.nio.file.Path;
2529
import java.nio.file.Paths;
2630
import java.util.ArrayList;
27-
import java.util.Arrays;
2831
import java.util.List;
29-
import java.util.concurrent.Callable;
3032

33+
@RunWith(PowerMockRunner.class)
34+
@PrepareForTest(TmcUtil.class)
3135
public class CourseInfoCommandTest {
3236

3337
private static final String COURSE_NAME = "2016-aalto-c";
@@ -40,8 +44,6 @@ public class CourseInfoCommandTest {
4044
private TestIo io;
4145
private TmcCore mockCore;
4246
private Course course;
43-
private Callable<List<Course>> callableList;
44-
private Callable<Course> callableCourse;
4547
private WorkDir workDir;
4648

4749
@BeforeClass
@@ -67,19 +69,7 @@ public void setUp() {
6769
exercises.get(0).setCompleted(true);
6870
course.setExercises(exercises);
6971

70-
callableList = new Callable<List<Course>>() {
71-
@Override
72-
public List<Course> call() throws Exception {
73-
return Arrays.asList(course);
74-
}
75-
};
76-
77-
callableCourse = new Callable<Course>() {
78-
@Override
79-
public Course call() throws Exception {
80-
return course;
81-
}
82-
};
72+
PowerMockito.mockStatic(TmcUtil.class);
8373
}
8474

8575
@Test
@@ -108,17 +98,15 @@ public void showErrorMessageIfNoCourseGivenWithIOption() {
10898

10999
@Test
110100
public void showMessageIfCourseDoesNotExistOnTheServer() {
111-
when(mockCore.listCourses(any(ProgressObserver.class))).thenReturn(callableList);
101+
when(TmcUtil.findCourse(eq(mockCore), eq("foo"))).thenReturn(null);
112102
String[] args = {"info", "foo", "-i"};
113103
app.run(args);
114104
assertTrue(io.out().contains("The course foo doesn't exist on this server"));
115105
}
116106

117107
@Test
118108
public void printCourseWithOptionI() {
119-
when(mockCore.listCourses(any(ProgressObserver.class))).thenReturn(callableList);
120-
when(mockCore.getCourseDetails(any(ProgressObserver.class),
121-
any(Course.class))).thenReturn(callableCourse);
109+
when(TmcUtil.findCourse(eq(mockCore), eq("test-course123"))).thenReturn(course);
122110

123111
String[] args = {"info", "test-course123", "-i"};
124112
app.run(args);
@@ -128,9 +116,7 @@ public void printCourseWithOptionI() {
128116

129117
@Test
130118
public void printCourseWithOptionsIAndA() {
131-
when(mockCore.listCourses(any(ProgressObserver.class))).thenReturn(callableList);
132-
when(mockCore.getCourseDetails(any(ProgressObserver.class),
133-
any(Course.class))).thenReturn(callableCourse);
119+
when(TmcUtil.findCourse(eq(mockCore), eq("test-course123"))).thenReturn(course);
134120

135121
String[] args = {"info", "test-course123", "-a", "-i"};
136122
app.run(args);
@@ -140,9 +126,7 @@ public void printCourseWithOptionsIAndA() {
140126
@Test
141127
public void printCourseWithNoExercisesFromTheServer() {
142128
course.setExercises(new ArrayList<Exercise>());
143-
when(mockCore.listCourses(any(ProgressObserver.class))).thenReturn(callableList);
144-
when(mockCore.getCourseDetails(any(ProgressObserver.class),
145-
any(Course.class))).thenReturn(callableCourse);
129+
when(TmcUtil.findCourse(eq(mockCore), eq("test-course123"))).thenReturn(course);
146130

147131
String[] args = {"info", "test-course123", "-i"};
148132
app.run(args);
@@ -192,9 +176,7 @@ public void printGivenCourseFromTheServerIfInCourseDirectoryAndGivenCourseName()
192176
app.setWorkdir(workDir);
193177

194178
course.setExercises(new ArrayList<Exercise>());
195-
when(mockCore.listCourses(any(ProgressObserver.class))).thenReturn(callableList);
196-
when(mockCore.getCourseDetails(any(ProgressObserver.class),
197-
any(Course.class))).thenReturn(callableCourse);
179+
when(TmcUtil.findCourse(eq(mockCore), eq("test-course123"))).thenReturn(course);
198180

199181
String[] args = {"info", "test-course123", "-i"};
200182
app.run(args);
@@ -206,9 +188,7 @@ public void printsErrorIfInCourseDirectoryAndGivenCourseNameThatDoesntExistOnThe
206188
workDir = new WorkDir(pathToDummyCourse);
207189
app.setWorkdir(workDir);
208190

209-
when(mockCore.listCourses(any(ProgressObserver.class))).thenReturn(callableList);
210-
when(mockCore.getCourseDetails(any(ProgressObserver.class),
211-
any(Course.class))).thenReturn(callableCourse);
191+
when(TmcUtil.findCourse(eq(mockCore), eq("test-course123"))).thenReturn(course);
212192

213193
String[] args = {"info", "notacourse", "-i"};
214194
app.run(args);

src/test/java/fi/helsinki/cs/tmc/cli/command/DownloadExercisesCommandTest.java

Lines changed: 23 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44
import static org.junit.Assert.assertTrue;
55
import static org.mockito.Matchers.any;
66
import static org.mockito.Matchers.anyListOf;
7+
import static org.mockito.Matchers.eq;
78
import static org.mockito.Mockito.doReturn;
89
import static org.mockito.Mockito.mock;
910
import static org.mockito.Mockito.spy;
1011
import static org.mockito.Mockito.when;
12+
import static org.powermock.api.mockito.PowerMockito.mockStatic;
1113

1214
import fi.helsinki.cs.tmc.cli.Application;
1315
import fi.helsinki.cs.tmc.cli.io.TestIo;
1416
import fi.helsinki.cs.tmc.cli.tmcstuff.Settings;
17+
import fi.helsinki.cs.tmc.cli.tmcstuff.TmcUtil;
1518
import fi.helsinki.cs.tmc.cli.tmcstuff.WorkDir;
1619
import fi.helsinki.cs.tmc.core.TmcCore;
1720
import fi.helsinki.cs.tmc.core.domain.Course;
@@ -27,15 +30,20 @@
2730
import org.junit.After;
2831
import org.junit.Before;
2932
import org.junit.Test;
33+
import org.junit.runner.RunWith;
34+
import org.powermock.core.classloader.annotations.PrepareForTest;
35+
import org.powermock.modules.junit4.PowerMockRunner;
3036

3137
import java.io.File;
3238
import java.io.IOException;
3339
import java.nio.file.Path;
3440
import java.nio.file.Paths;
3541
import java.util.ArrayList;
42+
import java.util.Arrays;
3643
import java.util.List;
37-
import java.util.concurrent.Callable;
3844

45+
@RunWith(PowerMockRunner.class)
46+
@PrepareForTest(TmcUtil.class)
3947
public class DownloadExercisesCommandTest {
4048

4149
Application app;
@@ -52,14 +60,15 @@ public void setUp() {
5260
app = new Application(io, workDir);
5361
mockCore = mock(TmcCore.class);
5462
app.setTmcCore(mockCore);
63+
64+
mockStatic(TmcUtil.class);
5565
}
5666

5767
@After
5868
public void tearDown() {
5969
try {
6070
FileUtils.deleteDirectory(tempDir.toFile());
61-
} catch (Exception e) {
62-
}
71+
} catch (Exception e) { }
6372
}
6473

6574
@Test
@@ -81,61 +90,26 @@ public void failIfCourseArgumentNotGiven() {
8190

8291
@Test
8392
public void worksRightIfCourseIsNotFound() throws IOException {
84-
Callable<List<Course>> callable = new Callable<List<Course>>() {
85-
@Override
86-
public List<Course> call() throws Exception {
87-
return new ArrayList<>();
88-
}
89-
};
90-
91-
when(mockCore.listCourses(any(ProgressObserver.class))).thenReturn(callable);
93+
when(TmcUtil.findCourse(eq(mockCore), eq("foo"))).thenReturn(null);
9294
String[] args = {"download", "foo"};
9395
app.run(args);
9496
io.assertContains("Course doesn't exist");
9597
}
9698

9799
@Test
98100
public void worksRightIfCourseIsFound() throws IOException {
99-
Callable<List<Course>> callableList = new Callable<List<Course>>() {
100-
@Override
101-
public List<Course> call() throws Exception {
102-
ArrayList<Course> tmp = new ArrayList<>();
103-
tmp.add(new Course("course1"));
104-
tmp.add(new Course("course2"));
105-
return tmp;
106-
}
107-
};
108-
109-
Callable<Course> callableCourse = new Callable<Course>() {
110-
@Override
111-
public Course call() throws Exception {
112-
Course course = new Course("course1");
113-
List<Exercise> lst = new ArrayList<>();
114-
lst.add(new Exercise("exercise"));
115-
course.setExercises(lst);
116-
return course;
117-
}
118-
};
119-
120-
Callable<List<Exercise>> callableExercise = new Callable<List<Exercise>>() {
121-
@Override
122-
public List<Exercise> call() throws Exception {
123-
ArrayList<Exercise> tmp = new ArrayList<>();
124-
tmp.add(new Exercise("exerciseName"));
125-
return tmp;
126-
}
127-
};
101+
Course course = new Course("course1");
102+
course.setExercises(Arrays.asList(new Exercise("exercise")));
103+
List<Exercise> exercises = Arrays.asList(new Exercise("exerciseName"));
104+
105+
when(TmcUtil.findCourse(eq(mockCore), eq("course1"))).thenReturn(course);
106+
when(TmcUtil.downloadExercises(eq(mockCore), anyListOf(Exercise.class),
107+
any(ProgressObserver.class))).thenReturn(exercises);
128108

129109
Settings settings = new Settings("server", "user", "password");
130110
settings.setTmcProjectDirectory(tempDir);
131111
app.setSettings(settings);
132112

133-
when(mockCore.listCourses(any(ProgressObserver.class))).thenReturn(callableList);
134-
when(mockCore.getCourseDetails(any(ProgressObserver.class),
135-
any(Course.class))).thenReturn(callableCourse);
136-
when(mockCore.downloadOrUpdateExercises(any(ProgressObserver.class),
137-
anyListOf(Exercise.class))).thenReturn(callableExercise);
138-
139113
String[] args = {"download", "course1"};
140114
app.run(args);
141115

@@ -153,14 +127,10 @@ public void filtersCompletedExercisesByDefault() throws ParseException {
153127
completed1.setCompleted(true);
154128
completed2.setCompleted(true);
155129

156-
List<Exercise> exercises = new ArrayList<>();
157-
exercises.add(completed1);
158-
exercises.add(notCompleted);
159-
exercises.add(completed2);
160-
161130
Course course = new Course("test-course");
162-
course.setExercises(exercises);
131+
course.setExercises(Arrays.asList(completed1, notCompleted, completed2));
163132

133+
/*TODO somehow get rid of this parser stuff */
164134
GnuParser parser = new GnuParser();
165135
CommandLine args = parser.parse(new Options(), new String[]{});
166136

@@ -189,6 +159,7 @@ public void getsAllExercisesWithAllSwitch() throws ParseException {
189159
Course course = new Course("test-course");
190160
course.setExercises(exercises);
191161

162+
/*TODO somehow get rid of this parser stuff */
192163
GnuParser parser = new GnuParser();
193164
Options options = new Options();
194165
options.addOption("a", "all", false, "");

src/test/java/fi/helsinki/cs/tmc/cli/command/ListCoursesCommandTest.java

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
package fi.helsinki.cs.tmc.cli.command;
22

3-
import static org.mockito.Matchers.anyObject;
3+
import static org.mockito.Matchers.eq;
44
import static org.mockito.Mockito.mock;
55
import static org.mockito.Mockito.when;
6+
import static org.powermock.api.mockito.PowerMockito.mockStatic;
67

78
import fi.helsinki.cs.tmc.cli.Application;
89
import fi.helsinki.cs.tmc.cli.io.TestIo;
10+
import fi.helsinki.cs.tmc.cli.tmcstuff.TmcUtil;
911
import fi.helsinki.cs.tmc.core.TmcCore;
1012
import fi.helsinki.cs.tmc.core.domain.Course;
11-
import fi.helsinki.cs.tmc.core.domain.ProgressObserver;
1213

1314
import org.junit.Before;
1415
import org.junit.Test;
16+
import org.junit.runner.RunWith;
17+
import org.powermock.core.classloader.annotations.PrepareForTest;
18+
import org.powermock.modules.junit4.PowerMockRunner;
1519

16-
import java.util.ArrayList;
20+
import java.util.Arrays;
1721
import java.util.List;
18-
import java.util.concurrent.Callable;
1922

23+
@RunWith(PowerMockRunner.class)
24+
@PrepareForTest(TmcUtil.class)
2025
public class ListCoursesCommandTest {
2126

2227
Application app;
@@ -29,6 +34,8 @@ public void setUp() {
2934
app = new Application(io);
3035
mockCore = mock(TmcCore.class);
3136
app.setTmcCore(mockCore);
37+
38+
mockStatic(TmcUtil.class);
3239
}
3340

3441
@Test
@@ -43,32 +50,19 @@ public void failIfCoreIsNull() {
4350

4451
@Test
4552
public void listCoursesWorksWithNoCourses() {
46-
Callable<List<Course>> callable = new Callable<List<Course>>() {
47-
@Override
48-
public List<Course> call() throws Exception {
49-
return new ArrayList<>();
50-
}
51-
};
52-
53-
when(mockCore.listCourses((ProgressObserver) anyObject())).thenReturn(callable);
53+
List<Course> list = Arrays.asList();
54+
when(TmcUtil.listCourses(eq(mockCore))).thenReturn(list);
55+
5456
String[] args = {"courses"};
5557
app.run(args);
5658
io.assertContains("No courses found on this server");
5759
}
5860

5961
@Test
6062
public void listCoursesWorksWithCourses() {
61-
Callable<List<Course>> callable = new Callable<List<Course>>() {
62-
@Override
63-
public List<Course> call() throws Exception {
64-
ArrayList<Course> tmp = new ArrayList<>();
65-
tmp.add(new Course("course1"));
66-
tmp.add(new Course("course2"));
67-
return tmp;
68-
}
69-
};
70-
71-
when(mockCore.listCourses((ProgressObserver) anyObject())).thenReturn(callable);
63+
List<Course> list = Arrays.asList(new Course("course1"), new Course("course2"));
64+
when(TmcUtil.listCourses(eq(mockCore))).thenReturn(list);
65+
7266
String[] args = {"courses"};
7367
app.run(args);
7468
io.assertContains("Found 2 courses");

0 commit comments

Comments
 (0)