English 中文(简体)
Mockito:如何期望一个炉子平等?
原标题:Mockito: how to expect an iterator to be equal?

我有一套办法,接受方言:

public int doSomething(String str, Iterator<String> itr)

我试图篡改这个方法的类别,这样,我会重新感到愤怒,视过去的论点而定。 在(时,我怎么能打电话给Mockito s,这样,它就期望通过这个编号的人“平等”到我具体说明的那个版本? 由于所通过的主持人是在系统中动态地建立起来的,因此,我不能在<代码>时(<>/代码>)使用同一实例。

List<String> aList = new ArrayList<String>();
aList.add("one");
aList.add("two");

MyClass myMock = Mockito.mock(MyClass.class);

我已尝试过,其中似乎没有任何工作:

Mockito.when(myMock.doSomething("some string", aList.iterator())).thenReturn(10);
Mockito.when(myMock.doSomething(Matchers.eq("some string"), Matchers.eq(aList.iterator()))).thenReturn(10);

我只有使用<代码>任何目标():

Mockito.when(myMock.doSomething(Matchers.eq("some string"), Matchers.anyObject())).thenReturn(10);

但是,当然,我无法根据当时的情况得出不同的结果。

最佳回答

你可以使用

when(myMock.doSomething(eq("some string"), argThat(new IsIteratorOfList(aList))))

rel=“nofollow”>ArgumentMatcher,其中检查:当给电离器的特性被添加到新名单时,该新名单等于构造者通过的名单(a 清单/编码。

配对人甚至可以更简单、公正地检查第一个要素,或检查内容的数量,或你想要的东西。

问题回答

页: 1 IterableMatcher<T>。 这包括静态方法<代码>elemEq。 这将为你们创造其中之一。

例如:

doReturn(result).when(mock).method(elemEq(expected));

您始终可以自行实施<代码>Iterator接口。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签