-
-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy pathJoobyRunnerTest.java
More file actions
186 lines (142 loc) · 6.45 KB
/
JoobyRunnerTest.java
File metadata and controls
186 lines (142 loc) · 6.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import java.util.*;
import java.util.function.Supplier;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import com.typesafe.config.Config;
import io.jooby.exception.StartupException;
import io.jooby.internal.MutedServer;
class JoobyRunnerTest {
private MockedStatic<Jooby> runnerMock;
private MockedStatic<ServerOptions> optionsMock;
private MockedStatic<MutedServer> mutedMock;
private Server server;
private ServerOptions serverOptions;
private ExecutionMode executionMode;
private List<Supplier<Jooby>> providers;
@BeforeEach
void setUp() {
// Mock static methods to isolate runApp
runnerMock = mockStatic(Jooby.class, Mockito.CALLS_REAL_METHODS);
optionsMock = mockStatic(ServerOptions.class);
mutedMock = mockStatic(MutedServer.class);
// Standard setup for method arguments
server = mock(Server.class);
executionMode = ExecutionMode.DEFAULT; // Assuming there is a DEFAULT, or mock it
providers = new ArrayList<>();
serverOptions = new ServerOptions(true);
when(server.getOptions()).thenReturn(serverOptions);
// Mock parseArguments to return an empty map by default to avoid polluting System properties
runnerMock.when(() -> Jooby.parseArguments(any())).thenReturn(Collections.emptyMap());
}
@AfterEach
void tearDown() {
runnerMock.close();
optionsMock.close();
mutedMock.close();
}
@Test
@DisplayName("Test Happy Path: Multiple apps, Muted Server, Defaults True")
void testRunApp_MultipleApps_MutedServer_DefaultsTrue() {
String[] args = new String[] {"arg1"};
// Setup MutedServer branch (loggerOff is NOT empty)
when(server.getLoggerOff()).thenReturn(List.of("SomeLogger"));
Server mutedServer = mock(Server.class);
mutedMock.when(() -> MutedServer.mute(server)).thenReturn(mutedServer);
// Setup multiple apps to cover the `if (appServerOptions == null)` branches
Supplier<Jooby> provider1 = mock(Supplier.class);
Supplier<Jooby> provider2 = mock(Supplier.class);
providers.add(provider1);
providers.add(provider2);
Jooby app1 = mock(Jooby.class);
Jooby app2 = mock(Jooby.class);
Config config1 = mock(Config.class);
when(app1.getConfig()).thenReturn(config1);
runnerMock.when(() -> Jooby.createApp(server, executionMode, provider1)).thenReturn(app1);
runnerMock.when(() -> Jooby.createApp(server, executionMode, provider2)).thenReturn(app2);
ServerOptions appOptions = new ServerOptions();
optionsMock.when(() -> ServerOptions.from(config1)).thenReturn(Optional.of(appOptions));
// Execution
Jooby.runApp(args, server, executionMode, providers);
// Verification
// Defaults was true, so server.setOptions should be called with appOptions
verify(server).setOptions(appOptions);
verify(mutedServer).start(new Jooby[] {app1, app2});
}
@Test
@DisplayName("Test Happy Path: Single app, Normal Server, Defaults False")
void testRunApp_SingleApp_NormalServer_DefaultsFalse() {
String[] args = new String[0];
// Setup Normal Server branch (loggerOff IS empty)
when(server.getLoggerOff()).thenReturn(Collections.emptyList());
// Set defaults to false to skip the override branch
serverOptions = new ServerOptions(false);
when(server.getOptions()).thenReturn(serverOptions);
Supplier<Jooby> provider1 = mock(Supplier.class);
providers.add(provider1);
Jooby app1 = mock(Jooby.class);
Config config1 = mock(Config.class);
when(app1.getConfig()).thenReturn(config1);
runnerMock.when(() -> Jooby.createApp(server, executionMode, provider1)).thenReturn(app1);
optionsMock.when(() -> ServerOptions.from(config1)).thenReturn(Optional.empty());
// Execution
Jooby.runApp(args, server, executionMode, providers);
// Verification
verify(server, never()).setOptions(any()); // Because defaults == false
verify(server).start(new Jooby[] {app1});
}
@DisplayName("Test Exception: StartupException thrown, stop throws ignored exception")
void testRunApp_StartupException_StopThrows() {
String[] args = new String[0];
when(server.getLoggerOff()).thenReturn(Collections.emptyList());
Supplier<Jooby> provider1 = mock(Supplier.class);
providers.add(provider1);
StartupException expectedException = new StartupException("Simulated startup failure");
// Force createApp to throw an exception
runnerMock
.when(() -> Jooby.createApp(server, executionMode, provider1))
.thenThrow(expectedException);
// Force targetServer.stop() to throw an exception to cover the `ignored` catch block
doThrow(new RuntimeException("Stop failed")).when(server).stop();
// Execution & Verification
StartupException thrown =
assertThrows(
StartupException.class, () -> Jooby.runApp(args, server, executionMode, providers));
assertEquals(expectedException, thrown);
verify(server).stop(); // Ensure stop was attempted
}
@Test
@DisplayName("Test Exception: Generic exception thrown, stop succeeds, wraps in StartupException")
void testRunApp_GenericException_StopSucceeds() {
String[] args = new String[0];
when(server.getLoggerOff()).thenReturn(Collections.emptyList());
Supplier<Jooby> provider1 = mock(Supplier.class);
providers.add(provider1);
Jooby app1 = mock(Jooby.class);
runnerMock.when(() -> Jooby.createApp(server, executionMode, provider1)).thenReturn(app1);
optionsMock.when(() -> ServerOptions.from(any())).thenReturn(Optional.empty());
RuntimeException genericException = new RuntimeException("Something bad happened");
// Force start() to throw a generic exception
doThrow(genericException).when(server).start(any());
// Execution & Verification
StartupException thrown =
assertThrows(
StartupException.class, () -> Jooby.runApp(args, server, executionMode, providers));
assertTrue(thrown.getMessage().contains("Application initialization resulted in exception"));
assertEquals(genericException, thrown.getCause());
// Verify stop succeeded gracefully
verify(server).stop();
}
}