From e151f45d55774486ca0fdfafca02d18033ccdd78 Mon Sep 17 00:00:00 2001 From: AlvoBen Date: Tue, 21 Jan 2025 10:33:15 +0200 Subject: [PATCH] add UT GlobalSettingsComponent --- .../global/GlobalSettingsComponentTest.java | 69 +++++++------------ 1 file changed, 25 insertions(+), 44 deletions(-) diff --git a/src/test/java/com/checkmarx/intellij/unit/settings/global/GlobalSettingsComponentTest.java b/src/test/java/com/checkmarx/intellij/unit/settings/global/GlobalSettingsComponentTest.java index fb8c57be..0df007c3 100644 --- a/src/test/java/com/checkmarx/intellij/unit/settings/global/GlobalSettingsComponentTest.java +++ b/src/test/java/com/checkmarx/intellij/unit/settings/global/GlobalSettingsComponentTest.java @@ -3,7 +3,6 @@ import com.checkmarx.intellij.settings.global.GlobalSettingsComponent; import com.checkmarx.intellij.settings.global.GlobalSettingsState; import com.checkmarx.intellij.settings.global.GlobalSettingsSensitiveState; -import com.checkmarx.intellij.settings.SettingsListener; import com.intellij.ide.passwordSafe.PasswordSafe; import com.intellij.openapi.application.Application; import com.intellij.openapi.application.ApplicationManager; @@ -14,7 +13,6 @@ import com.intellij.ui.components.JBPasswordField; import com.intellij.ui.components.fields.ExpandableTextField; import com.intellij.util.messages.MessageBus; -import com.intellij.util.messages.MessageBusConnection; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -56,34 +54,23 @@ class GlobalSettingsComponentTest { private GlobalSettingsComponent component; - private MockedStatic appManagerMock; - private MockedStatic settingsStateMock; - private MockedStatic sensitiveMock; - private MockedStatic editorColorsMock; - private MockedStatic passwordSafeMock; + private static MockedStatic appManagerMock; + private static MockedStatic settingsStateMock; + private static MockedStatic sensitiveMock; + private static MockedStatic editorColorsMock; + private static MockedStatic passwordSafeMock; @BeforeEach void setUp() throws Exception { - // Mock static methods - appManagerMock = mockStatic(ApplicationManager.class); - settingsStateMock = mockStatic(GlobalSettingsState.class); - sensitiveMock = mockStatic(GlobalSettingsSensitiveState.class); - editorColorsMock = mockStatic(EditorColorsManager.class); - passwordSafeMock = mockStatic(PasswordSafe.class); - - // Configure static mocks - appManagerMock.when(ApplicationManager::getApplication).thenReturn(mockApplication); - settingsStateMock.when(GlobalSettingsState::getInstance).thenReturn(mockSettingsState); - sensitiveMock.when(GlobalSettingsSensitiveState::getInstance).thenReturn(mockSensitiveState); - passwordSafeMock.when(PasswordSafe::getInstance).thenReturn(mockPasswordSafe); - editorColorsMock.when(EditorColorsManager::getInstance).thenReturn(mockEditorColorsManager); - // Configure instance mocks when(mockApplication.getMessageBus()).thenReturn(mockMessageBus); when(mockEditorColorsManager.getGlobalScheme()).thenReturn(mockEditorColorsScheme); when(mockEditorColorsScheme.getFont(any(EditorFontType.class))) .thenReturn(new Font(Font.MONOSPACED, Font.PLAIN, 12)); + // Mock static methods + mockStaticMethods(); + // Initialize static fields using reflection setStaticField(GlobalSettingsComponent.class, "SETTINGS_STATE", mockSettingsState); setStaticField(GlobalSettingsComponent.class, "SENSITIVE_SETTINGS_STATE", mockSensitiveState); @@ -92,18 +79,28 @@ void setUp() throws Exception { component = new GlobalSettingsComponent(); } + private void mockStaticMethods() { + if (appManagerMock == null) { + appManagerMock = mockStatic(ApplicationManager.class); + settingsStateMock = mockStatic(GlobalSettingsState.class); + sensitiveMock = mockStatic(GlobalSettingsSensitiveState.class); + editorColorsMock = mockStatic(EditorColorsManager.class); + passwordSafeMock = mockStatic(PasswordSafe.class); + + // Configure static mocks + appManagerMock.when(ApplicationManager::getApplication).thenReturn(mockApplication); + settingsStateMock.when(GlobalSettingsState::getInstance).thenReturn(mockSettingsState); + sensitiveMock.when(GlobalSettingsSensitiveState::getInstance).thenReturn(mockSensitiveState); + editorColorsMock.when(EditorColorsManager::getInstance).thenReturn(mockEditorColorsManager); + passwordSafeMock.when(PasswordSafe::getInstance).thenReturn(mockPasswordSafe); + } + } + @AfterEach void tearDown() throws Exception { // Reset static fields setStaticField(GlobalSettingsComponent.class, "SETTINGS_STATE", null); setStaticField(GlobalSettingsComponent.class, "SENSITIVE_SETTINGS_STATE", null); - - // Close static mocks - appManagerMock.close(); - settingsStateMock.close(); - sensitiveMock.close(); - editorColorsMock.close(); - passwordSafeMock.close(); } private void setStaticField(Class targetClass, String fieldName, Object value) throws Exception { @@ -112,22 +109,6 @@ private void setStaticField(Class targetClass, String fieldName, Object value field.set(null, value); } - @Test - void isModified_WithModifiedAdditionalParameters_ReturnsTrue() { - // Arrange - when(mockSettingsState.getAdditionalParameters()).thenReturn(""); - component.reset(); - - // Set text to additionalParametersField - component.getAdditionalParametersField().setText("modified"); - - // Act - boolean result = component.isModified(); - - // Assert - assertTrue(result); - } - @Test void reset_ResetsAllFields() { // Arrange