0

My testing method is failing. I'm not sure what to place in iterators place. Currently is null. How to test this when there is iterator?

Here is full main class which works, all it does is searching through array and returns index of the given number:

public class Main {
    public static void main(String[] args) {

    final List<Integer> numbers = Arrays.asList(3, 4, 6, 1, 9);
    final Integer x = Integer.valueOf(1);
    System.out.println(findSame(numbers.iterator(), x, 0));
}
public static final int findSame(Iterator<Integer> iterator, Integer x, int idx) {
    if (!iterator.hasNext()) {
        return -1;
    }

    if (iterator.next().equals(x)) {
        return idx;
    } else {
        return findSame(iterator, x, idx+1);
    }
    }
}

Here is my testing trial method, which is not functional.

@Test
public void searchNumReturnsIndex1(){
    Main instance = new Main();    

    System.out.println("Index");

    Iterator<Integer> iterator = null;

    int x = 6;

    int expResult = 2;
    int result = Main.findSame(iterator, x, 2);

    assertEquals(expResult, result);  
    }
6
  • 1
    Start by explaining what you're trying to do. Then explain where you're stuck. Then we might be able to help. Commented Aug 24, 2017 at 1:50
  • @shmosel Just did. Commented Aug 24, 2017 at 1:53
  • A bit clearer on what you're trying to do. But still not clear where you're stuck. Why is the unit test more difficult than the working code in main()? Commented Aug 24, 2017 at 1:55
  • Basically i don't know what to place here: int result = Main.findSame(iterator, x, 2); iterator or number.iterator? everything i try isn't really doing anything. Commented Aug 24, 2017 at 2:01
  • Well don't just try stuff at random; think. What's producing the iterator in main()? numbers. What is numbers? A list. Do you have a list in your unit test? Commented Aug 24, 2017 at 2:05

1 Answer 1

2

Your test should be testing something. Like,

  1. "if I have the list [2,3,5,7,11] and I search for 7, it should return 3"
  2. "if I have the list [2,3,5,7,11] and I search for 6, it should return -1"

Once you've decided what your tests should be, they should be short, simple things that test exactly that one case.

@Test
public void searchFor7InShortListOfPrimes() {
    List<Integer> numbers = Arrays.asList(2, 3, 5, 7, 11);
    assertEquals(3, Main.findSame(numbers.iterator(), 7, 0));  
}

@Test
public void searchFor6InShortListOfPrimes() {
    List<Integer> numbers = Arrays.asList(2, 3, 5, 7, 11);
    assertEquals(-1, Main.findSame(numbers.iterator(), 6, 0));  
}

But the tests should cover all possibilities. Like, what if the list is in descending order, or is not sorted at all. Whatever is appropriate for your method; don't test unsorted/descending sorted if your algorithm doesn't depend on that at all.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.