Skip to content

Commit c35593d

Browse files
committed
fix: handle ApplicationManager null in test environments
- Add null check in AutoDevSettingsState.getInstance() to return default instance when ApplicationManager is not available - Add try-catch blocks in AutoDevAppScope.scope() and workerScope() with fallback CoroutineScope instances - Remove @ignore annotation from LLMProvider2Test.shouldWorkWithJson as the test now passes - Fixes 'Cannot invoke Application.getService() because ApplicationManager.getApplication() is null' error in unit tests
1 parent e323739 commit c35593d

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

core/src/main/kotlin/cc/unitmesh/devti/settings/AutoDevSettingsState.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,13 @@ class AutoDevSettingsState : PersistentStateComponent<AutoDevSettingsState> {
6565
val language: String get() = getInstance().fetchLocalLanguage()
6666

6767
fun getInstance(): AutoDevSettingsState {
68-
return ApplicationManager.getApplication().getService(AutoDevSettingsState::class.java).state
68+
val application = ApplicationManager.getApplication()
69+
return if (application != null) {
70+
application.getService(AutoDevSettingsState::class.java).state
71+
} else {
72+
// Return a default instance for testing environments where ApplicationManager is not available
73+
AutoDevSettingsState()
74+
}
6975
}
7076
}
7177
}

core/src/main/kotlin/cc/unitmesh/devti/util/AutoDevCoroutineScope.kt

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,25 @@ class AutoDevAppScope: Disposable {
2424
}
2525

2626
companion object {
27-
fun scope(): CoroutineScope = service<AutoDevAppScope>().coroutineScope
28-
fun workerScope(): CoroutineScope = service<AutoDevAppScope>().workerScope
27+
fun scope(): CoroutineScope {
28+
return try {
29+
service<AutoDevAppScope>().coroutineScope
30+
} catch (e: Exception) {
31+
// Fallback for testing environments where ApplicationManager is not available
32+
CoroutineScope(SupervisorJob() + CoroutineExceptionHandler { _, throwable ->
33+
logger<AutoDevAppScope>().error(throwable)
34+
})
35+
}
36+
}
37+
38+
fun workerScope(): CoroutineScope {
39+
return try {
40+
service<AutoDevAppScope>().workerScope
41+
} catch (e: Exception) {
42+
// Fallback for testing environments where ApplicationManager is not available
43+
CoroutineScope(SupervisorJob() + workerThread)
44+
}
45+
}
2946
}
3047
}
3148

core/src/test/kotlin/cc/unitmesh/devti/llm2/LLMProvider2Test.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ class LLMProvider2Test {
3939
mockWebServer.shutdown()
4040
}
4141

42-
@Ignore("Cannot invoke \"com.intellij.openapi.application.Application.getService(java.lang.Class)\" because the return value of \"com.intellij.openapi.application.ApplicationManager.getApplication()\" is null")
4342
@Test
4443
fun shouldWorkWithJson() = runBlocking {
4544
val mockResponse = MockResponse()

0 commit comments

Comments
 (0)