Skip to main content



      Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Testing classes with injected IEclipsePreferences
Testing classes with injected IEclipsePreferences [message #1802412] Fri, 08 February 2019 08:20 Go to next message
Eclipse UserFriend
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.
Re: Testing classes with injected IEclipsePreferences [message #1802427 is a reply to message #1802412] Fri, 08 February 2019 11:39 Go to previous messageGo to next message
Eclipse UserFriend
Hi Ralf.

There are a few issues here. First, your Mockito `when` clause is missing a `do` action. You probably want something like (totally untested):
Mockito.when(preferences.getInt("foo", Mockito.anyInt())).doReturn(Integer.valueOf(4)));


But IIRC (and code in org.eclipse.e4.core.internal.di.InjectorImpl seems to confirm) the injector will see the `@Preference` annotation and only consult the corresponding preferences object supplier.

One way to work around this is to have a setter method that will receive the injected preference value, and call it manually in your test.
    @Inject
    @Preference(nodePath="/instance/certainlocation")
    @VisibleForTests /* guava annotation */
    void setEclipsePreferences(IEclipsePreferences eclipsePrefs) {
       someValue = eclipsePrefs.getInt("foo", Integer.valueOf(1));
    }


HTH
Re: Testing classes with injected IEclipsePreferences [message #1802435 is a reply to message #1802427] Fri, 08 February 2019 14:56 Go to previous message
Eclipse UserFriend
Hi Brian,
thanks for your fast answer. I thought I could omit the creation of the setter, but obviously it's not possible. After creating a setter for the preferences object I could get the tests run.

Regards,
Ralf.
Previous Topic:Remove Preferences Dialog - type filter text
Next Topic:Exchange (EMF-)Editor
Goto Forum:
  


Current Time: Sat Jul 05 06:18:53 EDT 2025

Powered by FUDForum. Page generated in 0.04249 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top