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
Post a Comment