Java 问题 - 获取与 Mockito 相关的错误

发布于 2024-08-12 02:37:11 字数 1209 浏览 4 评论 0原文

我使用 Mockito 库进行 Java 测试,当我运行测试时,Mockito 出现错误。 (我正在使用 NetBeans IDE,以防相关)。例如,我有一个名为 Animal 的类,我试图执行以下简单测试:

@Test
public void mokito_test(){

    Animal mockAnimal = mock(Animal.class);
    Animal testAnimal2 = mockAnimal;

    assertTrue(mockAnimal.equals(testAnimal2));

}

该测试给出以下错误:(

mokito_test caused an ERROR (at org.mockito.internal.creation.jmock.ClassImposterizer.<init>(ClassImposterizer.java:37))
  at org.mockito.internal.creation.jmock.ClassImposterizer.<init>(ClassImposterizer.java:37)
  at org.mockito.internal.util.CreationValidator.validateType(CreationValidator.java:14)
  at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:33)
...etc.

还有 11 个错误,包括 java.net、java.security、java.lang 和 sun .misc.)

但是,如果我使用真实对象而不是模拟对象执行此测试,则测试成功:

@Test
public void animal_test(){

    Animal testAnimal1 = new Animal("bear");
    Animal testAnimal2 = new Animal("bear");

    assertTrue(testAnimal1.equals(testAnimal2));

}

此测试成功。

我已将 Mockito jar 文件 (mockito-core-1.8.0.jar) 下载到我的项目目录中,然后在该特定项目的测试库中引用 jar 文件的相对路径。我以前从未使用过 Mockito,所以我怀疑我的错误与我的系统配置有关。任何帮助将不胜感激!谢谢!

I'm using the Mockito library for Java testing and getting errors in Mockito when I run my test. (I'm using the NetBeans IDE, in case that's relevant). For example, I have a class called Animal and I am trying to perform the following simple test:

@Test
public void mokito_test(){

    Animal mockAnimal = mock(Animal.class);
    Animal testAnimal2 = mockAnimal;

    assertTrue(mockAnimal.equals(testAnimal2));

}

This test gives the following error:

mokito_test caused an ERROR (at org.mockito.internal.creation.jmock.ClassImposterizer.<init>(ClassImposterizer.java:37))
  at org.mockito.internal.creation.jmock.ClassImposterizer.<init>(ClassImposterizer.java:37)
  at org.mockito.internal.util.CreationValidator.validateType(CreationValidator.java:14)
  at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:33)
...etc.

(There are 11 more errors, including in java.net, java.security, java.lang, and sun.misc.)

However, if I perform this test using a real object instead of a mocked object, the test is successfull:

@Test
public void animal_test(){

    Animal testAnimal1 = new Animal("bear");
    Animal testAnimal2 = new Animal("bear");

    assertTrue(testAnimal1.equals(testAnimal2));

}

This test is successfull.

I have downloaded the Mockito jar file (mockito-core-1.8.0.jar) into my project directory, and then referencing the relative path of the jar file in the testing library for this particular project. I've never used Mockito before, so I suspect that my error has something to do with my system configuration. Any help would be greatly appreciated! Thanks!

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

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

发布评论

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

评论(3

电影里的梦 2024-08-19 02:37:11

Mockito 对 objenesis 和 hamcrest 库有一些外部依赖,第 37 行尝试从 objensis 库构造一个对象。

请改用 mockito-all-1.8.jar

Mockito has some external dependencies on objenesis and hamcrest libraryes, line 37 attempts to construct an object from objensis library.

Please use mockito-all-1.8.jar instead.

a√萤火虫的光℡ 2024-08-19 02:37:11

您是否将mockito与junit test一起使用..不确定您要测试什么..
添加所需的 jar 文件(mockito-all jar)后,创建一个像这样的测试。因为您使用的是注释,所以您可以使用 @Mock 来创建模拟对象

    @RunWith(MockitoJUnitRunner.class)
    public class AnimalTest {

    @Mock
    private Animal mockAnimal;

    @Test
    public void mokito_test(){

        when(mockAnimal.toString()).thenReturn("Some String");

        String toStringResult = mockAnimal.toString();

        //verify(mockAnimal).toString();  -- to verify toString() method called once on the mock
        assertTrue("Some String".equals(toStringResult);

    }
}

如果您的 Animal 类使用对象 C 方法 b,那么您可以模拟对象 C 的方法,如上所示。

Are you using mockito with junit test.. was not sure what you were trying to test..
After adding the required jar file (mockito-all jar), create a test like this. since you are using annotation you can use @Mock to create a mock object

    @RunWith(MockitoJUnitRunner.class)
    public class AnimalTest {

    @Mock
    private Animal mockAnimal;

    @Test
    public void mokito_test(){

        when(mockAnimal.toString()).thenReturn("Some String");

        String toStringResult = mockAnimal.toString();

        //verify(mockAnimal).toString();  -- to verify toString() method called once on the mock
        assertTrue("Some String".equals(toStringResult);

    }
}

If your Animal class use Object C method b then you mock the method of the object C like shown above.

一张白纸 2024-08-19 02:37:11

您正在运行的测试不会测试任何内容,也不会尝试验证任何交互。模拟在面向对象的系统中用于指定对象如何彼此交互——以便检查一个对象是否告诉其他对象做某事。

它们不应该用于测试计算或值对象的状态更改。在这些类型的测试中使用模拟会导致脆弱的测试,因为您只是复制方法的底层实现。

因此,值对象上的方法(例如 equals、hashCode、toString)应始终使用真实对象进行测试。

这个过程在《Growing Object-Oriented Software, Guided by Tests》一书中有很好的描述。

The test you are running is not testing anything, nor is it trying to verify any interactions. Mocks are used in object orientated systems to specify how objects interact with each other - so to check that one object tells other objects to do things.

They shouldn't be used to test calculations, or state changes in value objects. Using mocks in these types of tests lead to brittle tests, because you are just duplicating the underlining implementation of your method.

So methods on value objects - such as equals, hashCode, toString, should always be tested using the real object.

This process is described very well in a book called "Growing Object-Oriented Software, Guided by Tests".

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