与 Mockito 一起在 junit 中进行的活动
我尝试测试由mockito模拟的javax.enterprise.event.Event,但抛出以下异常
Absent Code attribute in method that is not native or abstract in class file javax/enterprise/util/TypeLiteral
该类看起来像
public class MyClassTest {
@Mock Event<MyAlarm> event;
//...
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
//...
}
@Test
public void myTest() {
MyClass myClass = new MyClass();
myClass.event = event;
//...
verify(event, never()).fire(any(MyAlarm.class));
//...
}
}
我将以下内容添加到pom.xml(maven项目)中
<repository>
<id>glassfish</id>
<name>GlassFish Maven Repository</name>
<url>http://maven.glassfish.org/content/groups/glassfish</url>
</repository>
,并在javax( javaee-web-api) 依赖项
<dependency>
<groupId>org.glassfish.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
我做错了什么或者我误解了什么?
I try to test javax.enterprise.event.Event mocked by mockito but the following exception is thrown
Absent Code attribute in method that is not native or abstract in class file javax/enterprise/util/TypeLiteral
The class looks like
public class MyClassTest {
@Mock Event<MyAlarm> event;
//...
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
//...
}
@Test
public void myTest() {
MyClass myClass = new MyClass();
myClass.event = event;
//...
verify(event, never()).fire(any(MyAlarm.class));
//...
}
}
I add following to the pom.xml (maven project)
<repository>
<id>glassfish</id>
<name>GlassFish Maven Repository</name>
<url>http://maven.glassfish.org/content/groups/glassfish</url>
</repository>
and include the glassfish-embedded-all in front of the javax (javaee-web-api) dependency
<dependency>
<groupId>org.glassfish.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
What I have done wrong or what do I misunderstand?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我犯了一些小错误。本文Java EE 单元测试
Adam Bien 的 为我指明了正确的方向。
我删除了 javaee-web-api 依赖项并添加 glassfish-embedded-all 依赖项。我的 pom.xml 如下所示:
这对我来说效果很好,我可以测试 java ee 事件(或模拟它)。
I made some slight mistakes. This article Unit Testing for Java EE
by Adam Bien pointed me in the right direction.
I removed the javaee-web-api dependency and add the glassfish-embedded-all dependency. My
pom.xml
looks like:This works well for me and I can test java ee events (or mock-it out).
作为解决方法,我所做的就是在我的测试类中添加一个标记为 @Singleton 的内部类,充当事件观察者。这样的类看起来像这样:
然后在我的测试类中我注入这个类:
最后在测试方法中我会做类似的事情:
这对我有用。
As a workaround what I did was to add an inner class marked
@Singleton
in my test class that acts as an event observer. Such a class would look like this:And then in my test class I inject this class:
and finally in the test method I would do something like:
This worked for me.
TypeLiteral
中的方法是最终方法(请参阅 docs),这意味着 Mockito 无法模拟它们(请参阅 Mockito 常见问题解答)。也许考虑使用 PowerMock (我认为它可以与 Mockito 结合使用),它显然可以模拟 Final方法
The methods in
TypeLiteral
are final (see docs) which means that Mockito can not mock them (see the Mockito FAQ).Perhaps consider using PowerMock (it can work in conjunction with Mockito I think) which apparently can mock final methods