问题标签 [mockito]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
3 回答
748 浏览

java - 是否需要为这种 Mockito 情况编写自定义匹配器?

我有以下情况......我想在调用特定方法时抛出异常。我不在乎传递给此方法的内容,我只想调用一个异常。问题是此方法将自定义类作为参数。

我发现这样做的唯一方法是使用以下方法:

我尝试执行以下操作,但收到类似于“无法将对象转换为 Foo”的消息......这是有道理的:

我想知道是否有更简单的方法可以做到这一点,而不必为每个自定义类创建自定义匹配器?

谢谢

0 投票
2 回答
13503 浏览

java - Why is my Mockito-based unit test unable to run in Eclipse?

I have a Maven-managed project that uses Mockito mocking in its unit tests. I can run all the tests within a Maven build, and they run without error (and pass!). However, if I right-click a single test function, and choose "Run As -> JUnit Test", I get an exception java.lang.NoSuchMethodError: org.mockito.Mockito.doAnswer(Lorg/mockito/stubbing/Answer;)Lorg/mockito/stubbing/Stubber;. Of course, the "missing" method is there if I look at the sources, and like I said the tests compile and run from the command line.

Best I can think of is if Eclipse is trying to "help" me by providing an outdated Mockito artifact (I'm using 1.8.5 in my Maven dependencies) for the JUnit plugin, akin to how the Maven plugin can stick you with an oddball version of the Maven runtime for certain tasks.

Is this the problem? Is it something else? Can I fix this?

ETA: Apparently this may relate to a known issue. There's a good chance that it stems from having multiple versions of Mockito in my classpath (thanks, Maven :-/ ). I seem to have my house in order -- Eclipse can run the tests now -- but unfortunately the bug has bitten my Hudson. I have to track down how to remove the old JAR from the classpath there as well.

0 投票
2 回答
795 浏览

apache-flex - 可以使用 mockito-flex 模拟 getter 和 setter 吗?

需要知道 Mockito-flex 是否支持模拟 getter 和 setter。谢谢。

0 投票
2 回答
2702 浏览

java - 使用 mockito 测试登录 3 次会引发异常!

我有一个测试,但我不太擅长,我想验证当方法 login 被调用 3 次且用户名和密码错误时,它会引发异常,这个测试是否正确:

一个例子可能很好!

0 投票
2 回答
1178 浏览

java - 与工厂进行 Mockito 条件验证

在 mockito 中是否可以根据 mock 是否实际用于被测单元来验证在 mock 上调用了一个方法?

举个简单的例子,我为我的被测单元提供了一个模拟工厂(FooFactory),当调用 Foo.create() 时,它返回一个模拟(Foo)以供单元下的某些方法使用测试。我如何验证 Foo.method() 仅在被测单元调用 Foo.create() 时才被调用?

我设想代码看起来像这样:

举一个更具体的例子,我的工厂返回一个我想确保关闭的 Reader 对象,但并非类中的每个方法都实际构造了一个 Reader。我显然可以将验证添加到我知道需要 Reader 的每个测试中,但这似乎需要大量重复工作。

0 投票
1 回答
3939 浏览

unit-testing - 用 mockito 模拟 GWT EventBus

我在监视 EventBus 的真正 SimpleEventBus 实现时遇到了一些问题:我有一个活动,它也是特定事件的处理程序。此事件由服务触发。

编码:

错误跟踪在 eventBusSpy 验证中并说:

谢谢你。

0 投票
3 回答
2003 浏览

java - 如何使用 commons-fileupload 编写单元测试?

我正在使用 commons-fileupload 的框架中工作。

现在我需要编写单元测试类。我的疑问是如果 ServletFileUpload 不是接口,如何编写模拟测试?还有另一种方法可以测试我的课程吗?

昨天我使用 mockito 为 Servlet Parts 编写了单元测试,这很容易。但我想不出如何为 commons-fileupload 编写测试。

0 投票
7 回答
218832 浏览

java - 为什么 Mockito 不模拟静态方法?

我在这里阅读了一些关于静态方法的主题,我想我理解滥用/过度使用静态方法可能导致的问题。但我并没有真正深入了解为什么很难模拟静态方法。

我知道其他模拟框架,比如 PowerMock,可以做到这一点,但为什么 Mockito 不能呢?

我读了这篇文章,但作者似乎对这个词很虔诚static,也许是我的理解不佳。

一个简单的解释/链接会很棒。

0 投票
2 回答
8792 浏览

java - 为什么我的 Mockito 模拟对象使用真实的实现

我在模拟 Apache Http 客户端时遇到了问题。以下尝试创建模拟:

无法创建真正的模拟。上面的行毫无例外地执行,但是当我尝试存根某些行为时:

我从 AbstractHttpClient 中的一个方法中得到一个异常:

为什么调用在 Mockito.when 传递给 AbstractHttpClient 时执行?

我找到了解决这个特定问题的方法:使用接口 HttpClient 而不是试图模拟具体的子类。在这种情况下,这是一个更好的解决方案,但我想知道这里发生了什么?为什么我不能用 Mockito 正确地模拟这个具体的类?DefaultHttpClient 有什么特别之处吗?还有其他 Mockito 无法模拟具体类的情况吗?

我在 OSX 上使用 Mockito 1.8.5、Apache httpclient 4.0.3、Apache http core 4.1、JDK 1.6.0

0 投票
3 回答
38277 浏览

java - 为 Spring 单元测试注入模拟 @Service

我正在测试一个使用 @Autowired 注入服务的类:

但是如何在测试期间模拟 ruleStore?我不知道如何将我的模拟 RuleStore 注入 Spring 和自动装配系统。

谢谢