java JUnit 模拟,我应该使用哪个工具?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5418024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:01:25  来源:igfitidea点击:

JUnit mocks, which tool should i use?

javajunitmockingmockitojmock

提问by victorgp

I come from the PHP testing world and i'm starting testing in Java.

我来自 PHP 测试领域,我开始用 Java 进行测试。

I've seen several tools to mock the SUT in JUnit, like Mockito, SevenMock, ClassMock, etc.

我见过几种在 JUnit 中模拟 SUT 的工具,如 Mockito、SevenMock、ClassMock 等。

I really appreciate any recommendation of which one should i use.

我非常感谢我应该使用哪一个的任何建议。

Thanks in advance!

提前致谢!

回答by MalsR

I've used Mockito quite a lot. http://mockito.org/I have not used EasyMock so cant say much about it.

我经常使用 Mockito。http://mockito.org/我没有使用过 EasyMock 所以不能说太多。

Using Mockito is straightforward but the classes that you intend to test should also be in a decoupled state which will make it easier to test. With mockito you are instantiating a particular class with mocks objects.

使用 Mockito 很简单,但您打算测试的类也应该处于解耦状态,这将使测试更容易。使用 mockito,您正在使用模拟对象实例化一个特定的类。

Say you got a class that you want to test, but want to mock one of its dependencies

假设您有一个要测试的类,但想要模拟它的一个依赖项

final DepedencyToMockClass mockObject = mock(DepedencyToMockClass.class);
when(mockObject.getTestMethod()).thenReturn("Test");

Now this mockObject can now be injected when initializing your intended class.

现在可以在初始化您想要的类时注入这个模拟对象。

final ClassToTest test = new ClassToTest(mockObject);

Mockito uses reflection to create these mock objects. However if you have a dependency and if it is declared final then mocking will fail.

Mockito 使用反射来创建这些模拟对象。但是,如果您有一个依赖项并且如果它被声明为 final,那么模拟将失败。

Another useful method in Mockito is verify where you can verify certain operations in your mock objects. Have a peep at mockito. However there are limitations in mock objects, in some cases it will be hard to create mock objects perhaps external/third party code. I think it's good practise to attempt to instantiate real objects when injecting them for testing purposes, failing which Mockito helps.

Mockito 中另一个有用的方法是验证您可以在哪里验证模拟对象中的某些操作。看看mockito。然而,模拟对象存在局限性,在某些情况下,很难创建模拟对象,可能是外部/第三方代码。我认为在为测试目的注入真实对象时尝试实例化真实对象是一种很好的做法,如果失败,Mockito 会有所帮助。

回答by djodar

I've been using JMockfor a while. I personally like that the resulting code is easy to read, and you can easily differentiate between allowances and expectations:

我一直在使用JMock一段时间。我个人喜欢生成的代码易于阅读,并且您可以轻松区分津贴和期望:

context.checking(new Expectations() {{
        // allowances
        ignoring(parserState);
        allowing(factory).create(); will(returnValue(object));

        // expectations
        one(service).addSth(with(any(Integer.class)), with(sth));
    }});

Other powerful features are:

其他强大的功能是:

  • Sequences: invocations in sequence.
  • Argument matchers: hamcrest library
  • States: constraint invocations when a condition is true.
  • 序列:按顺序调用。
  • 参数匹配器:hamcrest 库
  • 状态:条件为真时的约束调用。