与生产环境相比,如何在Xtext测试中使用不同的对象注入?

lwi*_*lwi 3 eclipse dsl junit guice xtext

我尝试开始对中型Xtext项目进行单元测试。

生成器当前依赖于一些我想在测试中模拟的外部资源。因此,我将所需的对象通过@Inject注入Generator类。

例如在伪代码中:

class MyGenerator implements IGenerator{

@Inject
ExternalResourceInterface resourceInterface;

...

}
Run Code Online (Sandbox Code Playgroud)

我在语言RuntimeModule中创建了实际的绑定:

class MyRuntimeModule{
...
    @Override
    public void configure(Binder binder) {
        super.configure(binder);

        binder.bind(ExternalResourceInterface .class).to(ExternalResourceProductionAcess.class);
    }
...
}
Run Code Online (Sandbox Code Playgroud)

这对于生产环境而言效果很好。

但是,在生成器测试用例中,我想用我CompilationTestHelper的模拟版本替换绑定,以便对的以下调用使用模拟:

    compiler.assertCompilesTo(dsl, expectedJava);
Run Code Online (Sandbox Code Playgroud)

题:

我在哪里告诉guice / Xtext将注入绑定到模拟对象?

Zol*_*lyi 5

如果使用RunWith和InjectWith注释测试用例,则将通过特定的IInjectorProvider实现注入测试类。

如果该注入器提供程序使用自定义模块(如您所示),那么将使用该配置注入测试用例。但是,必须确保在整个测试代码中都使用该注射器(例如,您不依赖已注册的注射器等)。

查找以下代码作为示例(尚未编译,但这是您必须遵循的基本结构):

@RunWith(typeof(XtextRunner))
@InjectWith(typeof(LanguageInjectorProvider))
public class TestClass {

@Inject
CompilationTestHelper compiler

 ...
}
Run Code Online (Sandbox Code Playgroud)