Java: Testing a method with iterator -




my testing method failing. i'm not sure place in iterators place. null. how test when there iterator?

here full main class works, searching through array , returns index of 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 testing trial method, 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);       } 

your test should testing something. like,

  1. "if have list [2,3,5,7,11] , search 7, should return 3"
  2. "if have list [2,3,5,7,11] , search 6, should return -1"

once you've decided tests should be, should short, simple things test 1 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 tests should cover possibilities. like, if list in descending order, or not sorted @ all. whatever appropriate method; don't test unsorted/descending sorted if algorithm doesn't depend on @ all.





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -