【发布时间】:2015-12-29 00:55:59
【问题描述】:
我一直在处理的应用程序变得越来越复杂,以至于我在并发方面一遍又一遍地遇到相同的问题。解决相同的问题并且没有任何回归测试不再有意义。
那时我发现了 ThreadWeaver。对于我编写的一些简单的并发案例来说,这真的很棒,但是当我尝试用我的生产代码做一些更复杂的案例时,我开始感到沮丧。具体来说,当使用 Guice 注入组件时。
我有点难以理解 ThreadWeaver 运行测试的方式的含义,并在 wiki 文档中查找有关 Guice 或 DI 的任何提及,但没有运气。
Guice 与 ThreadWeaver 兼容吗?
这是我的测试
@Test
public void concurrency_test() {
AnnotatedTestRunner runner = new AnnotatedTestRunner();
runner.runTests(OPYLWeaverImpl.class, OPYLSurrogateTranscodingService.class);
}
这是我的测试实现
public class OPYLWeaverImpl extends WeaverFixtureBase {
@Inject private TaskExecutor taskExecutor;
@Inject private Serializer serializer;
@Inject private CountingObjectFileMarshaller liveFileMarshaller;
@Inject private GraphModel graphModel;
@Inject private CountingModelUpdaterService updaterService;
@Inject private BabelCompiler babelCompiler;
@Inject private EventBus eventBus;
OPYLSurrogateTranscodingService service;
private Path testPath;
@ThreadedBefore
public void before() {
service = new OPYLSurrogateTranscodingService(eventBus, taskExecutor, serializer, liveFileMarshaller,
() -> new OPYLSurrogateTranscodingService.Importer(graphModel, babelCompiler, updaterService, eventBus),
() -> new OPYLSurrogateTranscodingService.Validator(eventBus, babelCompiler),
() -> new OPYLSurrogateTranscodingService.Exporter(graphModel, updaterService));
}
@ThreadedMain
public void mainThread() {
testPath = FilePathOf.OASIS.resolve("Samples/fake-powershell-unit-test.opyl");
service.applyToExistingGraphModel(testPath);
}
@ThreadedSecondary
public void secondaryThread() {
}
@ThreadedAfter
public void after() {
}
还有WeaverFixtureBase
public class WeaverFixtureBase {
@Inject protected CountingEventBus eventBus;
@Before public final void setupComponents() {
Injector injector = Guice.createInjector(new WeaverTestingEnvironmentModule(CommonSerializationBootstrapper.class));
injector.getMembersInjector((Class) this.getClass()).injectMembers(this);
}
private class WeaverTestingEnvironmentModule extends AbstractModule {
private final Class<? extends SerializationBootstrapper> serializationBootstrapper;
public WeaverTestingEnvironmentModule(Class<? extends SerializationBootstrapper> serializationConfiguration) {
serializationBootstrapper = serializationConfiguration;
}
@Override protected void configure() {
bind(TaskExecutor.class).to(FakeSerialTaskExecutor.class);
bind(SerializationBootstrapper.class).to(serializationBootstrapper);
bind(ModelUpdaterService.class).toInstance(new CountingModelUpdaterService());
bindFactory(StaticSerializationConfiguration.Factory.class);
CountingEventBus localEventBus = new CountingEventBus();
bind(Key.get(EventBus.class, Bindings.GlobalEventBus.class)).toInstance(localEventBus);
bind(Key.get(EventBus.class, Bindings.LocalEventBus.class)).toInstance(localEventBus);
bind(CountingEventBus.class).toInstance(localEventBus);
bind(EventBus.class).toInstance(localEventBus);
}
@Provides
@Singleton
public GraphModel getGraphModel(EventBus eventBus, Serializer serializer) {
return MockitoUtilities.createMockAsInterceptorTo(new GraphModel(eventBus, serializer));
}
}
但是当类加载器加载OPYLWeaverImpl 时,Guice 的东西都没有发生,我得到一大堆空值。
我觉得这是“缺少一些非常简单”的场景之一。对不起,如果是!
【问题讨论】:
-
您确定 ThreadWeaver 尊重您的
@Before方法吗?据我所知,ThreadWeaver 中没有@Before,JUnit 仅尊重 TestCase 实现本身上的@Before和@After之类的注释——而不是由测试用例创建或使用的类。也许最好把它放在@ThreadedBefore方法中?
标签: java unit-testing testing guice thread-weaver