Skip to content

Commit 05d646e

Browse files
committed
fix: failing unit tests on server
1 parent 37affdf commit 05d646e

File tree

2 files changed

+57
-40
lines changed

2 files changed

+57
-40
lines changed

plugin/src/main/java/io/snyk/eclipse/plugin/Activator.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,14 @@ public static Activator getDefault() {
4646
* @return the image descriptor
4747
*/
4848
public static ImageDescriptor getImageDescriptor(String key) {
49-
return getDefault().getImageRegistry().getDescriptor(key);
49+
ImageRegistry imageRegistry = getDefault().getImageRegistry();
50+
51+
if (imageRegistry == null) {
52+
return ImageDescriptor.getMissingImageDescriptor();
53+
54+
}
55+
56+
return imageRegistry.getDescriptor(key);
5057
}
5158

5259
@Override
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.snyk.languageserver;
22

3+
import static org.mockito.ArgumentMatchers.anyString;
34
import static org.mockito.Mockito.clearAllCaches;
45
import static org.mockito.Mockito.mock;
56
import static org.mockito.Mockito.when;
@@ -8,51 +9,60 @@
89
import java.io.IOException;
910

1011
import org.eclipse.core.net.proxy.IProxyService;
12+
import org.eclipse.jface.resource.ImageRegistry;
13+
import org.eclipse.swt.graphics.Image;
1114
import org.junit.jupiter.api.AfterEach;
1215
import org.junit.jupiter.api.BeforeEach;
16+
import org.mockito.Mock;
17+
import org.mockito.MockitoAnnotations;
1318

1419
import io.snyk.eclipse.plugin.preferences.InMemoryPreferenceStore;
1520
import io.snyk.eclipse.plugin.preferences.Preferences;
1621
import io.snyk.eclipse.plugin.properties.preferences.PreferencesUtils;
1722

1823
public class LsBaseTest {
19-
protected LsRuntimeEnvironment environment = null;
20-
protected IProxyService proxyServiceMock;
21-
22-
private File lsFile = getTempFile();
23-
24-
@BeforeEach
25-
protected void setUp() {
26-
clearAllCaches();
27-
if (lsFile.exists())
28-
lsFile.delete();
29-
lsFile = getTempFile();
30-
environment = mock(LsRuntimeEnvironment.class);
31-
InMemoryPreferenceStore store = new InMemoryPreferenceStore();
32-
Preferences prefs = Preferences.getInstance(store);
33-
PreferencesUtils.setPreferences(prefs);
34-
prefs.setTest(true);
35-
36-
when(environment.getArch()).thenReturn("amd64");
37-
when(environment.getOs()).thenReturn("linux");
38-
when(environment.getDownloadBinaryName()).thenReturn("snyk-ls_testVersion_linux_amd64");
39-
proxyServiceMock = mock(IProxyService.class);
40-
when(environment.getProxyService()).thenReturn(proxyServiceMock);
41-
}
42-
43-
@AfterEach
44-
protected void tearDown() {
45-
lsFile.delete();
46-
clearAllCaches();
47-
}
48-
49-
protected File getTempFile() {
50-
try {
51-
File tempFile = File.createTempFile("ls-test", "tmp");
52-
tempFile.deleteOnExit();
53-
return tempFile;
54-
} catch (IOException e) {
55-
throw new RuntimeException(e);
56-
}
57-
}
24+
protected LsRuntimeEnvironment environment = null;
25+
protected IProxyService proxyServiceMock;
26+
27+
private File lsFile = getTempFile();
28+
@Mock
29+
private ImageRegistry imageRegistry;
30+
31+
@BeforeEach
32+
protected void setUp() {
33+
clearAllCaches();
34+
if (lsFile.exists())
35+
lsFile.delete();
36+
lsFile = getTempFile();
37+
environment = mock(LsRuntimeEnvironment.class);
38+
InMemoryPreferenceStore store = new InMemoryPreferenceStore();
39+
Preferences prefs = Preferences.getInstance(store);
40+
PreferencesUtils.setPreferences(prefs);
41+
prefs.setTest(true);
42+
43+
MockitoAnnotations.openMocks(this);
44+
when(imageRegistry.get(anyString())).thenReturn(mock(Image.class));
45+
46+
when(environment.getArch()).thenReturn("amd64");
47+
when(environment.getOs()).thenReturn("linux");
48+
when(environment.getDownloadBinaryName()).thenReturn("snyk-ls_testVersion_linux_amd64");
49+
proxyServiceMock = mock(IProxyService.class);
50+
when(environment.getProxyService()).thenReturn(proxyServiceMock);
51+
}
52+
53+
@AfterEach
54+
protected void tearDown() {
55+
lsFile.delete();
56+
clearAllCaches();
57+
}
58+
59+
protected File getTempFile() {
60+
try {
61+
File tempFile = File.createTempFile("ls-test", "tmp");
62+
tempFile.deleteOnExit();
63+
return tempFile;
64+
} catch (IOException e) {
65+
throw new RuntimeException(e);
66+
}
67+
}
5868
}

0 commit comments

Comments
 (0)