检票口和 AtUnit

发布于 2024-11-16 16:09:20 字数 1228 浏览 2 评论 0原文

我开始使用 Wicket,并选择 Guice 作为依赖项注入框架。现在我正在尝试学习如何为网页对象编写单元测试。

我用谷歌搜索了一下,找到了 这篇文章 但它提到了 AtUnit,所以我决定尝试一下。

我的 WebPage 类看起来像这样,

public class MyWebPage extends WebPage
{
    @Inject MyService service;
    public MyWebPage()
    {
        //here I build my components and use injected object.
        service.get(id);
        ....    
    }
}

我创建了模拟来用它替换任何生产 MyServiceImpl,我猜 Guice 与 AtUnit 一起应该注入它。

现在的问题是:

  1. AtUnit 期望我用 @Unit 标记目标对象 - 这没关系,因为我可以将已经创建的对象传递给 WicketTester

    @Unit MyWebPage 页面 = new MyWebPage();
    wicketTester.startPage(页面);
    

    但通常我会使用类名调用 startPage。

  2. 我认为 AtUnit 也期望目标对象是带有 @Inject 的市场,因此 AtUnit 可以创建和管理它 - 但我得到一个 org.apache.wicket.WicketRuntimeException: There is no application Attached to current thread main 。我可以指示 AtUnit 使用 wicketTester 中的应用程序吗?

  3. 因为我没有在 MyWebPage 中使用 @Inject (我认为),所以应该由 Guice 注入的所有对象都是 null (在我的示例中,服务引用为 null),

我真的在 Wicket 环境中找不到有关 AtUnit 的任何内容。我做错了什么吗,我错过了什么吗?

I've started playing with Wicket and I've chosen Guice as dependency injection framework. Now I'm trying to learn how to write a unit test for a WebPage object.

I googled a bit and I've found this post but it mentioned AtUnit so I decided to give it a try.

My WebPage class looks like this

public class MyWebPage extends WebPage
{
    @Inject MyService service;
    public MyWebPage()
    {
        //here I build my components and use injected object.
        service.get(id);
        ....    
    }
}

I created mock to replace any production MyServiceImpl with it and I guess that Guice in hand with AtUnit should inject it.

Now the problems are:

  1. AtUnit expects that I mark target object with @Unit - that is all right as I can pass already created object to WicketTester

    @Unit MyWebPage page = new MyWebPage();
    wicketTester.startPage(page);
    

    but usually I would call startPage with class name.

  2. I think AtUnit expects as well that a target object is market with @Inject so AtUnit can create and manage it - but I get an org.apache.wicket.WicketRuntimeException: There is no application attached to current thread main. Can I instruct AtUnit to use application from wicketTester?

  3. Because I don't use @Inject at MyWebPage (I think) all object that should be injected by Guice are null (in my example the service reference is null)

I really can't find anything about AtUnit inside Wicket environment. Am I doing something wrong, am I missing something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

韶华倾负 2024-11-23 16:09:20

我不知道 AtUnit,但我使用 wicket 和 guice 和 TestNG。我想 AtUnit 应该以同样的方式工作。重要的一点是使用 guice 创建 Web 应用程序。

下面是我如何将所有这些东西绑定在一起进行测试。

我有一个用于所有测试的抽象基类:

public abstract class TesterWicket<T extends Component> {

 @BeforeClass
    public void buildMockedTester() {
        System.out.println("TesterWww.buildMockedTester");
        injector = Guice.createInjector(buildModules());
        CoachWebApplicationFactory instance = 
            injector.getInstance(CoachWebApplicationFactory.class);
        WebApplication application = instance.buildWebApplication();
        tester = new WicketTester(application);
    }

protected abstract List<Module> buildModules();

每个测试类都完成初始化。子类在buildModules方法中定义了测试所需的模块。

在我的 IWebApplicationFactory 中,我添加了 GuiceComponentInjector。这样,在所有组件实例化之后,用 @Inject 注释的字段将由 Guice 填充:

public class CoachWebApplicationFactory implements IWebApplicationFactory {

  private static Logger LOG = LoggerFactory.getLogger(CoachWebApplicationFactory.class);

  private final Injector injector;

  @Inject
  public CoachWebApplicationFactory(Injector injector) {
      this.injector = injector;
  }

  public WebApplication createApplication(WicketFilter filter) {
    WebApplication app = injector.getInstance(WebApplication.class);
    Application.set(app);
    app.addComponentInstantiationListener(new GuiceComponentInjector(app, injector));
    return app;
  }
} 

I don't know AtUnit but I use wicket with guice and TestNG. I imagine that AtUnit should work the same way. The important point is the creation of the web application with the use of guice.

Here how I bind all this stuff together for my tests.

I have an abstract base class for all my tests:

public abstract class TesterWicket<T extends Component> {

 @BeforeClass
    public void buildMockedTester() {
        System.out.println("TesterWww.buildMockedTester");
        injector = Guice.createInjector(buildModules());
        CoachWebApplicationFactory instance = 
            injector.getInstance(CoachWebApplicationFactory.class);
        WebApplication application = instance.buildWebApplication();
        tester = new WicketTester(application);
    }

protected abstract List<Module> buildModules();

The initialization is done for every test class. The subclass defines the necessary modules for the test in the buildModules method.

In my IWebApplicationFactory I add the GuiceComponentInjector. That way, after all component instantiation, the fields annotated with @Inject are filled by Guice:

public class CoachWebApplicationFactory implements IWebApplicationFactory {

  private static Logger LOG = LoggerFactory.getLogger(CoachWebApplicationFactory.class);

  private final Injector injector;

  @Inject
  public CoachWebApplicationFactory(Injector injector) {
      this.injector = injector;
  }

  public WebApplication createApplication(WicketFilter filter) {
    WebApplication app = injector.getInstance(WebApplication.class);
    Application.set(app);
    app.addComponentInstantiationListener(new GuiceComponentInjector(app, injector));
    return app;
  }
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文