Skip to content

Commit 211f4f9

Browse files
committed
Extract method for mocking that GoblintServer is alive
1 parent 7e8d245 commit 211f4f9

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

src/test/java/GoblintAnalysisTest.java

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,22 @@ class GoblintAnalysisTest {
4848
@SystemStub
4949
private SystemOut systemOut;
5050

51+
/**
52+
* A function to mock that GoblintServer is alive
53+
* and Goblint's configuration file is ok.
54+
*/
55+
private void mockGoblintServerIsAlive(GoblintServer goblintServer) {
56+
doReturn(true).when(goblintServer).isAlive();
57+
when(goblintConfWatcher.refreshGoblintConfig()).thenReturn(true);
58+
}
59+
5160
/*
5261
Mock test to ensure @analyze function
5362
messages user when analyzes fails
5463
*/
5564
@Test
5665
void analyzeFailed() {
57-
// Mock that GoblintServer is alive and everything is fine with Goblint's configuration file
58-
doReturn(true).when(goblintServer).isAlive();
59-
when(goblintConfWatcher.refreshGoblintConfig()).thenReturn(true);
66+
mockGoblintServerIsAlive(goblintServer);
6067

6168
// Mock that the analyses of Goblint have started and completed
6269
when(goblintService.analyze(new AnalyzeParams(false))).thenReturn(CompletableFuture.failedFuture(new Throwable(" Testing failed analysis")));
@@ -85,9 +92,7 @@ void abortAnalysis() throws IOException {
8592
GoblintServer goblintServer = mock(GoblintServer.class);
8693
GoblintAnalysis goblintAnalysis = new GoblintAnalysis(magpieServer, goblintServer, goblintService, gobPieConfiguration, goblintConfWatcher);
8794

88-
// Mock that GoblintServer is alive and everything is fine with Goblint's configuration file
89-
doReturn(true).when(goblintServer).isAlive();
90-
when(goblintConfWatcher.refreshGoblintConfig()).thenReturn(true);
95+
mockGoblintServerIsAlive(goblintServer);
9196

9297
// Mock that the analyses of Goblint have started but not completed (still run)
9398
CompletableFuture<GoblintAnalysisResult> runningProcess = new CompletableFuture<>();
@@ -116,9 +121,7 @@ void abortAnalysisFails() throws IOException {
116121
GoblintServer goblintServer = mock(GoblintServer.class);
117122
GoblintAnalysis goblintAnalysis = new GoblintAnalysis(magpieServer, goblintServer, goblintService, gobPieConfiguration, goblintConfWatcher);
118123

119-
// Mock that GoblintServer is alive and everything is fine with Goblint's configuration file
120-
doReturn(true).when(goblintServer).isAlive();
121-
when(goblintConfWatcher.refreshGoblintConfig()).thenReturn(true);
124+
mockGoblintServerIsAlive(goblintServer);
122125

123126
// Mock that the analyses of Goblint have started but not completed (still run)
124127
CompletableFuture<GoblintAnalysisResult> runningProcess = new CompletableFuture<>();
@@ -147,9 +150,7 @@ void abortAnalysisFails() throws IOException {
147150

148151
@Test
149152
void preAnalyseTest() {
150-
// Mock that GoblintServer is alive and everything is fine with Goblint's configuration file
151-
doReturn(true).when(goblintServer).isAlive();
152-
when(goblintConfWatcher.refreshGoblintConfig()).thenReturn(true);
153+
mockGoblintServerIsAlive(goblintServer);
153154

154155
// A process that must be run before analysis
155156
String processPrintout = "'Hello'";
@@ -177,9 +178,7 @@ void preAnalyseTest() {
177178
*/
178179
@Test
179180
void preAnalyseEmpty() {
180-
// Mock that GoblintServer is alive and everything is fine with Goblint's configuration file
181-
doReturn(true).when(goblintServer).isAlive();
182-
when(goblintConfWatcher.refreshGoblintConfig()).thenReturn(true);
181+
mockGoblintServerIsAlive(goblintServer);
183182

184183
// Mock that Goblint returns some messages
185184
when(goblintService.messages()).thenReturn(CompletableFuture.completedFuture(new ArrayList<>()));
@@ -208,9 +207,7 @@ void preAnalyseEmpty() {
208207
*/
209208
@Test
210209
void preAnalyseNull() {
211-
// Mock that GoblintServer is alive and everything is fine with Goblint's configuration file
212-
doReturn(true).when(goblintServer).isAlive();
213-
when(goblintConfWatcher.refreshGoblintConfig()).thenReturn(true);
210+
mockGoblintServerIsAlive(goblintServer);
214211

215212
// Mock that Goblint returns some messages
216213
when(goblintService.messages()).thenReturn(CompletableFuture.completedFuture(new ArrayList<>()));
@@ -238,9 +235,7 @@ void preAnalyseNull() {
238235
*/
239236
@Test
240237
void preAnalyseError() {
241-
// Mock that GoblintServer is alive and everything is fine with Goblint's configuration file
242-
doReturn(true).when(goblintServer).isAlive();
243-
when(goblintConfWatcher.refreshGoblintConfig()).thenReturn(true);
238+
mockGoblintServerIsAlive(goblintServer);
244239

245240
// Mock that Goblint returns some messages
246241
when(goblintService.messages()).thenReturn(CompletableFuture.completedFuture(new ArrayList<>()));

0 commit comments

Comments
 (0)