与 Mockito 一起在 junit 中进行的活动

发布于 2024-11-25 11:31:47 字数 1524 浏览 2 评论 0原文

我尝试测试由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 技术交流群。

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

发布评论

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

评论(3

冰雪梦之恋 2024-12-02 11:31:47

我犯了一些小错误。本文Java EE 单元测试
Adam Bien 的
为我指明了正确的方向。
我删除了 javaee-web-api 依赖项并添加 glassfish-embedded-all 依赖项。我的 pom.xml 如下所示:

    <!-- .... -->
    <dependency>
        <groupId>org.glassfish.extras</groupId>
        <artifactId>glassfish-embedded-all</artifactId>
        <version>3.1.1</version>
        <scope>provided</scope>
    </dependency>
    <!-- <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>6.0</version>
        <scope>provided</scope>
    </dependency>-->
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>1.8.5</version>
        <scope>test</scope>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
        <scope>test</scope>
    </dependency>
    <!-- .... -->
  <repositories>
    <repository>
        <url>http://download.java.net/maven/2/</url>
        <id>java.net</id>
        <layout>default</layout>
        <name>java.net</name>
    </repository>
</repositories>

这对我来说效果很好,我可以测试 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:

    <!-- .... -->
    <dependency>
        <groupId>org.glassfish.extras</groupId>
        <artifactId>glassfish-embedded-all</artifactId>
        <version>3.1.1</version>
        <scope>provided</scope>
    </dependency>
    <!-- <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>6.0</version>
        <scope>provided</scope>
    </dependency>-->
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>1.8.5</version>
        <scope>test</scope>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
        <scope>test</scope>
    </dependency>
    <!-- .... -->
  <repositories>
    <repository>
        <url>http://download.java.net/maven/2/</url>
        <id>java.net</id>
        <layout>default</layout>
        <name>java.net</name>
    </repository>
</repositories>

This works well for me and I can test java ee events (or mock-it out).

梦里兽 2024-12-02 11:31:47

作为解决方法,我所做的就是在我的测试类中添加一个标记为 @Singleton 的内部类,充当事件观察者。这样的类看起来像这样:

@Singleton
public static class MyEventObserver {
    MyEvent event;

    public void onMyEvent(@Observes MyEvent event) {
            this.event = event;
        }
    }
}

然后在我的测试类中我注入这个类:

@Inject MyEventObserver observer;

最后在测试方法中我会做类似的事情:

Assert.assertNotNull(observer.event);

这对我有用。

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:

@Singleton
public static class MyEventObserver {
    MyEvent event;

    public void onMyEvent(@Observes MyEvent event) {
            this.event = event;
        }
    }
}

And then in my test class I inject this class:

@Inject MyEventObserver observer;

and finally in the test method I would do something like:

Assert.assertNotNull(observer.event);

This worked for me.

滥情稳全场 2024-12-02 11:31:47

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文