-1

How to write test case for this function in a binary search tree ?

void insert(String key){

    root=insertRec(root,key);
}
Gowtham Bk
  • 61
  • 1
  • 1
  • 6
  • 1
    Possible duplicate of [JUNIT testing void methods](http://stackoverflow.com/questions/16043819/junit-testing-void-methods) – kryger Jan 31 '16 at 09:28

1 Answers1

1

Your method does something. It obviously changes the state of the object by inserting a rec(ord?) and somehow re-evaluating what the root is. So, to test it, you should somehow be able to determine the new state, for example...

public void insert_should_create_new_record_and_set_root() {
     assertThat( myObject.getRec( originalRoot) ).isNull();

     Object originalRoot = myObject.getRoot();
     myObject.insert("xyz");

     assertThat( myObject.getRec( originalRoot) ).isEqualTo( "xyz"); // using AssertJ style here
     assertThat( myObject.getRoot().value() ).isNotEqualTo( originalRoot );      
}

If, on the other hand, you have no way to check the state from the outside, then you'll have a problem. But somehow your class has to communicate to the outside, hasn't it? If you really think that you cannot check the new state, then you'll have to provide more code of this class, as this answer is, of course, very general (which means "guessing", here).

Florian Schaetz
  • 10,454
  • 5
  • 32
  • 58