I want to mock the singleton object? It seems that it's impossible in scalamock-3
I found that easyMock and powerMock can mock the singleton object (from https://github.com/fabura/scala-MockStaticObjects)
However, I cant get this work? Any ideas?
I want to mock the singleton object? It seems that it's impossible in scalamock-3
I found that easyMock and powerMock can mock the singleton object (from https://github.com/fabura/scala-MockStaticObjects)
However, I cant get this work? Any ideas?
If the singleton object has a static .getInstance method, it is fairly easy to mock.
You need to do the following at the top of your test class
@RunWith(PowerMockRunner.class)
@PrepareForTest(Singleton.class)
then to mock the singleton
mockStatic(Singleton.class);
Singleton mockSingleton = mock(Singleton.class);
when(Singleton.getInstance()).thenReturn(mockSingleton);
any values you want to have the returned singleton to have I believe you have to set prior to the "when" Example: when(mockSingleton.toString()).thenReturn("I'm a mock");