当前位置: 首页 > 知识库问答 >
问题:

如何从测试中注入上下文

丁良骏
2023-03-14

我正在测试一个(Eclipse 4)应用程序(我不是在谈论单元测试,而是更多的集成和系统测试)。

我有一个反复出现的问题需要解决。我必须将测试中的上下文“注入”(@inject)到被测试的类中。换句话说,我需要测试做应用程序通常做的事情。

我所做的是创建一个私有方法:

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;
}

我(错误地)期望刚才在这里创建的上下文可以在测试中的一个类中使用。例如。:

class MyDBClassToTest {
   @Inject
   private IEclipseContext context;

   @Inject
   private IEventBroker broker;
   // ... etc
}

肯定少了些什么!我也创建了activator(为简洁起见,在实现下面没有注释)。。。但是没有帮助:

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);
    }
}

任何想法、暗示或建议?

共有2个答案

顾跃
2023-03-14

您可以使用任何其他可以注入私有字段的轻量级注入框架。

例如,将Mockito与@Spy(允许注入真实对象,而不仅仅是mock)和@injectmock一起使用。请参见此答案以获取示例。

或者编写自己的微型注射器。下面是EJB的一个示例(在文章的底部),但是您可以调整它以使用Eclipse注释。

晏兴发
2023-03-14

你需要在你的上下文中使用ContextInjsight Factory.make来创建你想要测试的类:

ContextInjectionFactory.make(MyDBClassToTest.class, tempContext);

或者,您可以使用ContextInjectionFactory.inject向已构造的类中注入:

MyDbClassToTest myClass = new MyDbClassToTest();

ContextInjectionFactory.inject(myClass, tempContext);

只注入使用这些方法之一创建的类。

 类似资料:
  • 问题内容: 我是新来的。我试图针对使用JDK 1.7,Spring 3.1,Groovy 1.8.6,Spock 0.6,Maven 3.0.4的独立Java应用程序编写Spock单元测试。基本的hello world spock测试正在运行。但是,当我尝试测试弹簧豆时,我发现它们没有被注入。我使用这里提到的方法。在 when 块内 , businessObjectDao为null 。我该如何工作

  • 我有需要测试的REST服务。该服务具有Spring Security身份验证,我需要在测试或模拟中关闭它。我决定嘲笑它,因为我不能关掉它。我为此编写了,但现在未加载上下文: 在我的主要源代码中,我有一些config类加载了一些其他bean,而it类在我的测试中没有加载,我有一个例外: 我做错了什么?有人能帮我吗?我使用了,但在该版本中,无法工作,因为属性不再存在。

  • Summary NoSQL databases provide looser consistency restrictions than traditional SQL databases. By requiring fewer relational constraints and consistency checks, NoSQL databases often offer performanc

  • Summary In this section, some SQL Injection techniques for PostgreSQL will be discussed. These techniques have the following characteristics: PHP Connector allows multiple statements to be executed by

  • Summary SQL Injection vulnerabilities occur whenever input is used in the construction of a SQL query without being adequately constrained or sanitized. The use of dynamic SQL (the construction of SQL

  • Summary Web based PL/SQL applications are enabled by the PL/SQL Gateway, which is is the component that translates web requests into database queries. Oracle has developed a number of software impleme