简体   繁体   English

Junit实现Iterable

[英]Junit implements Iterable

I am trying to do tests on a class that implements Iterable. 我正在尝试对实现Iterable的类进行测试。 When I go to test any method,in the itorator class, like hasNext(), next(). 当我去测试任何方法时,在itorator类中,例如hasNext(),next()。 So when I call list.itorator().Next(); 所以当我打电话给list.itorator()。Next(); nothing happens, the node is the current node in the list not the next. 没有任何反应,该节点是列表中的当前节点,而不是下一个。 but when I do ListIterator itrList = new ListIterator(list); 但是当我做ListIterator itrList = new ListIterator(list); the code inside it works. 其中的代码有效。 I am calling public Iterator iterator() {return new ListIterator (this); 我正在调用public Iterator iterator(){返回新的ListIterator(this); So what am I not doing correctly/ grasping? 那我在做什么/不正确地掌握呢? Thanks for any help. 谢谢你的帮助。

public void test6() {
    List<String> list = new List<String>();

    list.addAfterCurrent("1");
    list.addAfterCurrent("2");
    list.addAfterCurrent("3");
    ListIterator itrList = new ListIterator(list);


    TextIO.putln(list.iterator().next()); // output is 1
    TextIO.putln(list.iterator().next()); // output is 1 
    TextIO.putln(itrList.next());         // output is 1
    TextIO.putln(itrList.next());         // now output is 2           
    assertEquals("1", list.getCurrent());
    assertEquals(3, list.size());

}

You're creating a fresh iterator each time you call list.iterator() . 每次调用list.iterator()时,您都在创建一个新的迭代器。 You want to keep the same iterator and call its next method multiple times, eg 您想要保留相同的迭代器并多次调用其next方法,例如

Iterator itr = list.iterator();
assertEquals(1, itr.next());
assertEquals(2, itr.next());

您每次都在创建一个新的迭代器,并从头开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在实现Iterable的LinkedList中使用泛型 - Using Generic in LinkedList that implements Iterable junit测试Iterable的相等性 - junit testing equality of an Iterable 如何使用Junit测试实现Runnable的类 - How to test class which implements Runnable with Junit 是否等于Java类中的实现,该类实现了扩展Iterable的接口? - Equals implementation in a java-class which implements an interface that extends Iterable? 泛型并在实现Iterable接口的类中使用新创建的方法 - Generics and using newly created methods in a Class that implements Iterable Interface 如何为实现 ApplicationListener 的 class 编写 JUNIT 测试用例<applicationpreparedevent></applicationpreparedevent> - How to write JUNIT test case for a class which implements ApplicationListener<ApplicationPreparedEvent> 如何使用Java反射来检查给定的类是实现Iterable <? extends T> ,对于任何给定的T - How to use Java reflection to check a given class is implements Iterable<? extends T>, for any given T 当操作类实现 ParameterAware 时,无法从 Junit 中的请求中获取数据 - Not able to get data from request in Junit when action class implements ParameterAware 实现 Closeable 或实现 AutoCloseable - implements Closeable or implements AutoCloseable Scala可迭代和Java可迭代 - Scala iterable and java iterable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM