检票口和 AtUnit
我开始使用 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 一起应该注入它。
现在的问题是:
AtUnit 期望我用 @Unit 标记目标对象 - 这没关系,因为我可以将已经创建的对象传递给 WicketTester
@Unit MyWebPage 页面 = new MyWebPage(); wicketTester.startPage(页面);
但通常我会使用类名调用 startPage。
我认为 AtUnit 也期望目标对象是带有 @Inject 的市场,因此 AtUnit 可以创建和管理它 - 但我得到一个 org.apache.wicket.WicketRuntimeException: There is no application Attached to current thread main 。我可以指示 AtUnit 使用 wicketTester 中的应用程序吗?
- 因为我没有在 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:
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.
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?
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道 AtUnit,但我使用 wicket 和 guice 和 TestNG。我想 AtUnit 应该以同样的方式工作。重要的一点是使用 guice 创建 Web 应用程序。
下面是我如何将所有这些东西绑定在一起进行测试。
我有一个用于所有测试的抽象基类:
每个测试类都完成初始化。子类在buildModules方法中定义了测试所需的模块。
在我的 IWebApplicationFactory 中,我添加了
GuiceComponentInjector
。这样,在所有组件实例化之后,用 @Inject 注释的字段将由 Guice 填充: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:
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: