Testing classes with injected IEclipsePreferences [message #1802412] |
Fri, 08 February 2019 08:20  |
Eclipse User |
|
|
|
Hi all,
I want to test a class which gets an IEclipsePreferences object injected. Unfortunately, I can't mock that object (with Mockito). Here's what I've done:
my class to test:
@Singleton
public final class MyClass {
@Inject
@Preference(nodePath="/instance/certainlocation")
private IEclipsePreferences eclipsePrefs;
...
public Double theMethodToTest() {
...
eclipsePrefs.getInt("foo", Integer.valueOf(1)); // if foo doesn't exist use number 1
...
}
}
And here's my test class:
public class MyClassTest {
@Mock
protected IEclipsePreferences preferences;
MyClass myClass;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
Mockito.when(preferences.getInt("foo", Integer.valueOf(4)));
context = EclipseContextFactory.create("mockedContext");
context.set(IEclipsePreferences.class, preferences);
ContextInjectionFactory.setDefault(context);
myClass= ContextInjectionFactory.make(MyClass.class, context);
}
@Test
public void somethingToTest() {
myClass.theMethodToTest();
}
Within the tested class the call to preferences always returns "1", not "4" as expected by setting this value through Mockito call. Is there another possibility to mock the preferences service? Should I register my own (mock) service at a certain bundle?
Any hints are welcome.
Yours,
Ralf.
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04249 seconds