我一直在处理的应用程序变得越来越复杂,并且已经达到了我并发地反复遇到相同问题的地步。解决相同的问题并且没有任何回归测试不再有意义。

那是我找到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的所有内容都没有消失,并且我得到了大量的null。

我觉得这是那些“缺少某些东西而非常简单”的场景之一。不好意思!

最佳答案

以上评论是正确的。线程编织器与JUnit完全无关。线程编织器是它自己的运行程序,它根据自己的注释执行测试用例。您不得在Thread Weaver测试中使用任何特定于JUnit的注释。

除此之外,Thread Weaver不需要特定框架的任何兼容性。它操纵Java字节代码,并使用aperate类加载器加载操纵的代码。

最后,没有任何辅助测试的Thread Weaver测试没有任何意义。线程编织器通过交错单独的执行路径来工作。没有第二个线程,Thread Weaver只能单步执行单个线程,而不会添加任何值。

10-05 23:02
查看更多