您使用什么技术来调试复杂的guice绑定? [英] What techniques do you use to debug complex guice bindings?

查看:192
本文介绍了您使用什么技术来调试复杂的guice绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组复杂的绑定,其中包含许多私有绑定来解决机器人腿问题

I have a complex set of bindings that include many private bindings to solve the robot legs problem.

由于Guice能够报告可理解的BIDE错误,我想知道除了阅读Guice的运行时异常之外,还有什么有效的工具或技术(如果有的话)可用于排除运行时绑定问题错误。

Because of Guice's limited ability to report intelligible biding errors, I'm wondering what effective tools or techniques, if any, besides reading Guice's runtime exceptions are available to troubleshoot runtime binding errors.

逐步执行配置代码是没有帮助的,因为配置发生在引导时,而不是在通常发生错误的对象实例化时间。

Stepping through configuration code isn't helpful, because the configuration happens at boot time rather than at object instantiation time, where errors usually occur.

如果Guice图形插件运行起来,Guice图形插件可能会很有用 - 我的实验结果导致了不正确的图形。

The Guice graph plugin would likely be useful if it worked--my experiments with it have resulted in incorrect graphs.

推荐答案

我发现以下两个提示可用于从此答案进行调试:

I found the following two tips useful for debugging from this answer:

  • Grapher visualizes injectors. If your custom provider implements HasDependencies, it can augment this graph.
  • Binder.skipSources() lets you to write extensions whose error messages track line numbers properly.

Binder.skipSources()如果您编写通用绑定助手方法很有用,而Guice只报告行号的通用助手方法,但你(很可能)实际上希望调用者的行号一级升高到堆栈。

Binder.skipSources() is useful if you write generic binding helper methods and Guice only reports the line number of the generic helper method, but you (most likely) actually want the line number of the caller one level up the stack instead.

我正在为Android开发,所以从修改绑定的时间到构建时间可能相当慢,直到我看到我在设备或模拟器上的更改结果。所以我开发了单元测试,将直接在主机PC上验证Guice绑定。即使您不是针对Android开发的,编写Guice绑定单元测试可能会有所帮助,如下所示。现在,我看起来像这样(这里在Scala - Java将看起来相似)

I'm developing for Android, so the build time can be quite slow from the time I modify my bindings until I see the results of my changes on the device or simulator. So I've developed unit tests that will verify the Guice bindings directly on the host PC. Even if you're not developing for Android, it can be helpful to write Guice binding unit tests as follows. Right now, mine look something like this (here in Scala--Java would look similar)

class ProviderTest {
    var injector : Injector = null

    @Before
    def setUp() {
        injector = Guice.createInjector(
            new BindModule1,
            new BindModule2,
            new BindGlobals
            )
    }

    @After
    def tearDown()  {
    }

    @Test   def InjectedClass1WasBound()  {
        val provider = injector.getProvider(classOf[InjectedClass1])
    }

    @Test   def InjectedClass2WasBound() {
        val provider = injector.getProvider(classOf[InjectedClass2])
    }   
}

我从最深刻的班级。即,如果将C注入到B中,注入到A中,我将在C开始测试。如果单元测试C的绑定失败,我将开始注释C中的注入字段,直到获得绑定成功。然后我将注意力层次重新转移到这个过程中。

I write tests starting from the most deeply-bound class. I.e., if C is injected into B, which is injected into A, I'll start testing at C. If unit testing C's binding fails, I'll start commenting out the injected fields in C until I get the binding to succeed. Then I move my way up the injection hierarchy repeating this process.

当然,如果你遵循测试驱动的开发,并确保包括全覆盖的Guice绑定测试在您的套件中,一旦您打破绑定,您就会发现这些错误。

Of course if you follow test-driven development, and make sure to include full-coverage Guice-binding tests in your suite, you'll detect these errors as soon as you break a binding.

这篇关于您使用什么技术来调试复杂的guice绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆