Avoid duplicating code when writing junit code for a method called by another one -


i'm new junit. have 2 methods in tested class. method call method b. in b, there condition cases need cover when writing test case.

so, in case, if write test b, a, tested code duplicated. have idea case?

the code looks like:

class example{  public void a(){   assert b();   vara ++;  }  public boolean b(){  if (case1){   var1b ++;   if (case 1.1){     var2b++;     return false;   } }  var3b --; return true; }  } 

thanks.

use mockito (or mocking framework), mock method b when testing a, , make return want:

example example = spy(new example()); when(example.b()).thenreturn(true);  // call a() , test should when b() returns true. 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -