Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » How to Inject a context from a test(Making the context created in a test available to the whole application)
How to Inject a context from a test [message #1696233] Sun, 24 May 2015 08:59
Kasper M.Friend
Messages: 15
Registered: September 2014
Junior Member
I'm testing an (Eclipse 4) application (I'm not talking about unit test but more integration and system-test).

I've a recurrent problem I need to solve. I have to "inject" (@Inject) a context from the test into the class(es) under test. In other words I need the test does what the application usually do.

What I've done is creating a private method:

private IEclipseContext createApplicationContext() {
    IEclipseContext tempContext = E4Application.createDefaultContext();
    ContextInjectionFactory.make(CommandServiceAddon.class, tempContext);
    ContextInjectionFactory.make(ContextServiceAddon.class, tempContext);
    eventBroker = (IEventBroker) tempContext.get(IEventBroker.class.getName());
    tempContext.set(IEventBroker.class, eventBroker);
    return tempContext;
}
I expected (wrongly) that the context just created here would have been made available in one of the classes under test. E.g.:

class MyDBClassToTest {
@Inject
private IEclipseContext context;

@Inject
private IEventBroker broker;
// ... etc
}
for sure there is something missing! I've created the activator too (below the implementation without comment for brevity) ... but didn't help:

import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

public class Activator extends AbstractUIPlugin {

    // The shared instance
    private static Activator plugin;

    // The plug-in ID
    public static final String PLUGIN_ID = "my.path....";

    public static Activator getDefault() {
        return plugin;
    }

    public Activator() {
    }

    @Override
    public void start(BundleContext context) throws Exception {
        super.start(context);
        plugin = this;
    }

    @Override
    public void stop(BundleContext context) throws Excepti`enter code here`on {
        plugin = null;
        super.stop(context);
    }
}


or ... is it possible from the test to "get the application" object and set the context there so that to make it available to the whole application in the context of my test?

any idea, hint or suggestion?
thanks a lot!
Previous Topic:Variables view does not show name only value
Next Topic:PropertySheet - set selection when editor is being open
Goto Forum:
  


Current Time: Tue Mar 19 03:20:40 GMT 2024

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

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

Back to the top